"use strict";

exports.__esModule = true;
exports.default = void 0;

var _tslib = require("tslib");

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _meetReactComponentOne = require("@alifd/meet-react-component-one");

var _button = _interopRequireDefault(require("../button"));

var _view = _interopRequireDefault(require("../view"));

var _form = require("../form");

var _selectDrawer = _interopRequireDefault(require("../select-drawer"));

var _safeArea = _interopRequireDefault(require("../safe-area"));

var _selectBody = _interopRequireDefault(require("./section/select-body"));

var _selectSearch = _interopRequireDefault(require("./section/select-search"));

var _selectedListDrawer = _interopRequireDefault(require("./section/selected-list-drawer"));

var _locale2 = require("../locale");

var _zhCn = _interopRequireDefault(require("../locale/lang/zh-cn"));

var _utils = require("../utils");

var _hooks = require("../utils/hooks");

var _context = _interopRequireDefault(require("./context"));

var _util = require("./util");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

var BaseSelect = function (props, ref) {
  var _classNames;

  var _props$prefix = props.prefix,
      prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
      placeholder = props.placeholder,
      _value = props.value,
      _props$type = props.type,
      type = _props$type === void 0 ? 'normal' : _props$type,
      defaultValue = props.defaultValue,
      className = props.className,
      _props$mode = props.mode,
      mode = _props$mode === void 0 ? 'single' : _props$mode,
      hasSearch = props.hasSearch,
      children = props.children,
      _dataSource = props.dataSource,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      hasClear = props.hasClear,
      onSearch = props.onSearch,
      _props$displayType = props.displayType,
      displayType = _props$displayType === void 0 ? 'normal' : _props$displayType,
      _props$filterLocal = props.filterLocal,
      filterLocal = _props$filterLocal === void 0 ? true : _props$filterLocal,
      _props$filter = props.filter,
      filter = _props$filter === void 0 ? _util.defaultLocalFilter : _props$filter,
      renderSelection = props.renderSelection,
      useDetailValue = props.useDetailValue,
      _props$onChange = props.onChange,
      onChange = _props$onChange === void 0 ? function () {} : _props$onChange,
      _props$onClear = props.onClear,
      onClear = _props$onClear === void 0 ? function () {} : _props$onClear,
      _props$onCancel = props.onCancel,
      onCancel = _props$onCancel === void 0 ? function () {} : _props$onCancel,
      _locale = props.locale,
      sizeProp = props.size,
      _props$notFoundConten = props.notFoundContent,
      notFoundContent = _props$notFoundConten === void 0 ? '' : _props$notFoundConten,
      others = (0, _tslib.__rest)(props, ["prefix", "placeholder", "value", "type", "defaultValue", "className", "mode", "hasSearch", "children", "dataSource", "disabled", "hasClear", "onSearch", "displayType", "filterLocal", "filter", "renderSelection", "useDetailValue", "onChange", "onClear", "onCancel", "locale", "size", "notFoundContent"]);

  var _useContext = (0, _react.useContext)(_form.ItemContext),
      isInsideForm = _useContext.isInsideForm,
      contextSize = _useContext.size;

  var size = sizeProp || (isInsideForm ? contextSize : 'medium');

  var _useState = (0, _react.useState)(''),
      searchVal = _useState[0],
      setSearchVal = _useState[1];

  var selectLocale = (0, _locale2.useLocale)('Select', props, _zhCn.default.Select);
  var selectDrawerLocal = (0, _locale2.useLocale)('SelectDrawer', {}, _zhCn.default.SelectDrawer);
  var clsPrefix = prefix + "select";
  var baseSelectRef = (0, _react.useRef)(null);
  var dataSource = (0, _react.useMemo)(function () {
    return (0, _utils.getDataSource)(props.dataSource, props.children);
  }, [props.dataSource, props.children]);

  var compare = function (a, b) {
    return (0, _util.isEqualItems)(a, b, true);
  };

  var _useValue = (0, _hooks.useValue)(props, [], {
    fitValue: function fitValue(v) {
      if (v === '' || v === null) {
        return [];
      }

      var x = Array.isArray(v) ? v : [v];

      if (useDetailValue) {
        return x;
      } else if ((0, _utils.isValidArray)(x)) {
        return x.map(function (item) {
          var tmp = (0, _utils.findInArray)(dataSource, function (d) {
            return d.value === item;
          });
          return tmp || {
            label: item,
            value: item
          };
        });
      } else {
        return [];
      }
    },
    compare: compare
  }),
      value = _useValue[0],
      setValue = _useValue[1],
      isControlled = _useValue[2];

  var _useState2 = (0, _react.useState)(value),
      innerValue = _useState2[0],
      setInnerValue = _useState2[1];

  var _useState3 = (0, _react.useState)(false),
      confirmVisible = _useState3[0],
      setConfirmVisible = _useState3[1];

  var isSingle = mode === 'single';
  var searchRef = (0, _react.useRef)(null);
  var realHasSearch = props.hasSearch || props.showSearch;
  (0, _react.useMemo)(function () {
    if (isControlled && !compare(value, innerValue)) {
      setInnerValue(value);
    }
  }, [value]);
  var handleCancel = (0, _react.useCallback)(function (reason) {
    setInnerValue(value);

    if (searchRef && searchRef.current) {
      searchRef.current.setValue('');
    }

    if (baseSelectRef && baseSelectRef.current) {
      baseSelectRef.current.hide();
    }

    if ((0, _utils.isFunction)(onCancel)) {
      onCancel(reason);
    }
  }, [value]);

  var handleOk = function () {
    if (!isControlled) {
      setValue(innerValue);
    }

    var changed = !(0, _util.isEqualItems)(value, innerValue);

    if (changed && (0, _utils.isFunction)(onChange)) {
      var _getValidEvtValues = (0, _util.getValidEvtValues)({
        selectedItems: innerValue,
        isSingle: isSingle,
        useDetailValue: useDetailValue
      }),
          val = _getValidEvtValues.value,
          items = _getValidEvtValues.items;

      onChange(val, 'change', items);
    }

    if (searchRef && searchRef.current) {
      searchRef.current.setValue('');
    }

    if (baseSelectRef && baseSelectRef.current) {
      baseSelectRef.current.hide();
    }
  };

  var contextValue = (0, _react.useMemo)(function () {
    return Object.assign(Object.assign({}, props), {
      hasSearch: realHasSearch,
      dataSource: dataSource,
      type: type,
      mode: mode,
      size: size,
      prefix: prefix,
      value: value,
      innerValue: innerValue,
      displayType: displayType,
      locale: selectLocale,
      onChange: function (v) {
        return setInnerValue(v);
      },
      searchValue: searchVal,
      setSearchValue: setSearchVal
    });
  }, [props, realHasSearch, dataSource, type, mode, size, prefix, value, innerValue, displayType, selectLocale, searchVal]);
  (0, _react.useImperativeHandle)(ref, function () {
    return Object.assign(baseSelectRef.current);
  });
  return /*#__PURE__*/(0, _react.createElement)(_react.Fragment, null, /*#__PURE__*/(0, _react.createElement)(_selectDrawer.default, _extends({}, others, {
    size: size,
    ref: baseSelectRef,
    showToolbar: !realHasSearch,
    type: type,
    disabled: disabled,
    placeholder: placeholder,
    hasClear: hasClear,
    className: (0, _classnames.default)(clsPrefix, className),
    content: renderSelection ? renderSelection(value) : (0, _util.innerRenderSelection)(value, contextValue),
    onClear: function handleClear() {
      if (!isControlled) {
        setValue([]);
        setInnerValue([]);

        if (searchRef && searchRef.current) {
          searchRef.current.setValue('');
        }
      }

      if ((0, _utils.isFunction)(onClear)) {
        onClear();
      }

      if ((0, _utils.isFunction)(onChange)) {
        var _getValidEvtValues2 = (0, _util.getValidEvtValues)({
          selectedItems: [],
          useDetailValue: useDetailValue,
          isSingle: isSingle
        }),
            val = _getValidEvtValues2.value,
            items = _getValidEvtValues2.items;

        onChange(val, 'clear', items);
      }
    },
    onCancel: handleCancel,
    onOk: handleOk,
    drawerClassName: (0, _classnames.default)((_classNames = {}, _classNames[clsPrefix + "--searchable"] = realHasSearch, _classNames))
  }), /*#__PURE__*/(0, _react.createElement)(_context.default.Provider, {
    value: contextValue
  }, /*#__PURE__*/(0, _react.createElement)(_react.Fragment, null, realHasSearch && /*#__PURE__*/(0, _react.createElement)(_selectSearch.default, {
    ref: searchRef
  }), dataSource.length === 0 && !realHasSearch ? /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clsPrefix + "-drawer-content " + clsPrefix + "drawer-drawer--empty"
  }, (0, _utils.isString)(notFoundContent) ? /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
    className: clsPrefix + "drawer-drawer-empty-text"
  }, notFoundContent) : /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clsPrefix + "drawer-drawer-empty-text"
  }, notFoundContent)) : /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.ScrollView, {
    className: clsPrefix + "-option-list"
  }, /*#__PURE__*/(0, _react.createElement)(_selectBody.default, null)), realHasSearch && /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clsPrefix + "-drawer-footer"
  }, !isSingle && /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clsPrefix + "-footer-counter",
    onClick: function handleTotalClick() {
      setConfirmVisible(true);
    }
  }, /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, null, selectLocale.totalPrefixText), /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
    className: clsPrefix + "-total-num"
  }, innerValue.length), /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, null, selectLocale.totalSuffixText)), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clsPrefix + "-drawer-btn-wrapper"
  }, /*#__PURE__*/(0, _react.createElement)(_button.default, {
    size: "large",
    className: clsPrefix + "-footer-button",
    type: "primary",
    model: "outline",
    onClick: function onClick() {
      return handleCancel('cancel-button');
    }
  }, selectDrawerLocal.cancel), /*#__PURE__*/(0, _react.createElement)(_button.default, {
    size: "large",
    className: clsPrefix + "-footer-button",
    type: "primary",
    model: "solid",
    onClick: handleOk
  }, selectDrawerLocal.ok)), /*#__PURE__*/(0, _react.createElement)(_safeArea.default, null))))), !isSingle && /*#__PURE__*/(0, _react.createElement)(_selectedListDrawer.default, {
    prefix: contextValue.prefix,
    innerValue: contextValue.innerValue,
    locale: contextValue.locale,
    visible: confirmVisible,
    onOk: function handleConfirmDone(items) {
      setInnerValue(items);
      setConfirmVisible(false);
    }
  }));
};

BaseSelect.displayName = 'SelectDrawer';

var _default = /*#__PURE__*/(0, _react.forwardRef)(BaseSelect);

exports.default = _default;