import { Cascader } from 'antd';
import type { DefaultOptionType } from 'antd/es/cascader';
import { CHINA_REGION_OPTIONS } from '../lib/china-region';

type ChinaRegionCascaderProps = {
  value?: string[];
  onChange?: (codes: string[]) => void;
  disabled?: boolean;
  placeholder?: string;
};

export default function ChinaRegionCascader({
  value,
  onChange,
  disabled,
  placeholder = '请选择省 / 市 / 区县',
}: ChinaRegionCascaderProps) {
  return (
    <Cascader
      options={CHINA_REGION_OPTIONS as DefaultOptionType[]}
      value={value}
      onChange={(codes) => onChange?.((codes ?? []) as string[])}
      disabled={disabled}
      placeholder={placeholder}
      showSearch={{
        filter: (input, path) =>
          path.some((option) =>
            String(option.label ?? '').toLowerCase().includes(input.toLowerCase()),
          ),
      }}
      changeOnSelect={false}
    />
  );
}
