import "antd/es/cascader/style";
import _Cascader from "antd/es/cascader";
var _excluded = ["options", "fieldNames", "value", "level", "onChange"];

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

import React, { useState, useEffect, useMemo, useRef, useImperativeHandle } from 'react';
import { isArray, last } from 'lodash-es';
import { getCitysCode, global } from '@parallel-line/utils';
var levelMap = ['province', 'city', 'district'];
var CitySelect = /*#__PURE__*/React.forwardRef(function (_ref, ref) {
  var _ref$options = _ref.options,
      options = _ref$options === void 0 ? global.countryTree : _ref$options,
      fieldNames = _ref.fieldNames,
      val = _ref.value,
      _ref$level = _ref.level,
      level = _ref$level === void 0 ? 'district' : _ref$level,
      onChange = _ref.onChange,
      props = _objectWithoutProperties(_ref, _excluded);

  var cascaderRef = useRef(null);

  var _ref2 = fieldNames || {},
      _ref2$children = _ref2.children,
      key = _ref2$children === void 0 ? 'districts' : _ref2$children,
      _ref2$label = _ref2.label,
      label = _ref2$label === void 0 ? 'name' : _ref2$label,
      _ref2$value = _ref2.value,
      v = _ref2$value === void 0 ? 'adcode' : _ref2$value;

  var _useState = useState(getCitysCode(val)),
      _useState2 = _slicedToArray(_useState, 2),
      value = _useState2[0],
      setValue = _useState2[1];

  useEffect(function () {
    setValue(getCitysCode(val));
  }, [val]);

  function filter(inputValue, path) {
    return path.some(function (option) {
      var _option$label, _option$label$toLower, _option$label$toLower2, _option$label$toLower3, _inputValue$toLowerCa;

      return (option === null || option === void 0 ? void 0 : (_option$label = option[label]) === null || _option$label === void 0 ? void 0 : (_option$label$toLower = _option$label.toLowerCase) === null || _option$label$toLower === void 0 ? void 0 : (_option$label$toLower2 = (_option$label$toLower3 = _option$label$toLower.call(_option$label)).indexOf) === null || _option$label$toLower2 === void 0 ? void 0 : _option$label$toLower2.call(_option$label$toLower3, inputValue === null || inputValue === void 0 ? void 0 : (_inputValue$toLowerCa = inputValue.toLowerCase) === null || _inputValue$toLowerCa === void 0 ? void 0 : _inputValue$toLowerCa.call(inputValue))) > -1;
    });
  }

  var optionsFormat = useMemo(function () {
    if (!level || level === 'district') return options;
    var citycodes = ["021", "023", "022", "010"];
    var index = levelMap.indexOf(level);
    var levelArray = levelMap.slice(0, index);

    var loop = function loop(data) {
      return data.map(function (t) {
        if (!levelArray.includes(t.level)) return _objectSpread(_objectSpread({}, t), {}, _defineProperty({}, key, undefined));
        return _objectSpread(_objectSpread({}, t), {}, _defineProperty({}, key, isArray(t[key]) && t[key].length > 0 && !citycodes.includes(t.citycode) ? loop(t[key]) : undefined));
      });
    };

    return loop(options);
  }, [options, level]);
  useImperativeHandle(ref, function () {
    return {
      focus: function focus() {
        var _cascaderRef$current;

        (_cascaderRef$current = cascaderRef.current) === null || _cascaderRef$current === void 0 ? void 0 : _cascaderRef$current.focus();
      },
      blur: function blur() {
        var _cascaderRef$current2;

        (_cascaderRef$current2 = cascaderRef.current) === null || _cascaderRef$current2 === void 0 ? void 0 : _cascaderRef$current2.blur();
      }
    };
  });

  var handleChange = function handleChange(value) {
    setValue(value);
    onChange === null || onChange === void 0 ? void 0 : onChange(last(value));
  };

  return /*#__PURE__*/React.createElement(_Cascader, Object.assign({
    ref: cascaderRef,
    changeOnSelect: true,
    value: value,
    onChange: handleChange,
    showSearch: {
      filter: filter
    },
    fieldNames: {
      children: key,
      value: v,
      label: label
    },
    options: optionsFormat
  }, props));
});
export default CitySelect;