"use strict";

function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

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

var _one = require("remax/one");

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

var _indexModule = _interopRequireDefault(require("./styles/index.module.less"));

var _icon = _interopRequireDefault(require("../icon"));

var _fold = _interopRequireDefault(require("../fold"));

var _rotate = _interopRequireDefault(require("../rotate"));

var _space = _interopRequireDefault(require("../space"));

var _configProvider = require("../config-provider");

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; }

/* eslint-disable @typescript-eslint/no-explicit-any */
var _default = function _default(props) {
  var options = props.options,
      onToggle = props.onToggle,
      showOptions = props.showOptions,
      title = props.title,
      value = props.value,
      icon = props.icon,
      arrowsCls = props.arrowsCls,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      _props$bgColor = props.bgColor,
      bgColor = _props$bgColor === void 0 ? '#fff' : _props$bgColor,
      _props$arrowsColor = props.arrowsColor,
      arrowsColor = _props$arrowsColor === void 0 ? '#bbb' : _props$arrowsColor,
      _props$arrowsSize = props.arrowsSize,
      arrowsSize = _props$arrowsSize === void 0 ? '20px' : _props$arrowsSize,
      _onTap = props.onTap,
      onChange = props.onChange,
      titleCls = props.titleCls,
      children = props.children;

  var _useConfig = (0, _configProvider.useConfig)(),
      brandPrimary = _useConfig.brandPrimary;

  var selectItem = (0, _react.useMemo)(function () {
    return options === null || options === void 0 ? void 0 : options.find(function (item) {
      return item.value === value;
    });
  }, [options, value]);

  var new_style = function new_style() {
    var item_styles = {
      color: brandPrimary,
      backgroundColor: bgColor ? bgColor : '#fff'
    };

    if (disabled) {
      item_styles['color'] = '#bbb';
    }

    return item_styles;
  };

  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement(_fold.default, {
    folded: !showOptions,
    className: _indexModule.default.down,
    maxHeight: '50vh'
  }, /*#__PURE__*/_react.default.createElement(_space.default, {
    vertical: true,
    style: {
      width: '100%'
    }
  }, options === null || options === void 0 ? void 0 : options.map(function (item) {
    return /*#__PURE__*/_react.default.createElement(_one.View, {
      key: item.value,
      onTap: function onTap() {
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
        // @ts-ignore
        onToggle === null || onToggle === void 0 ? void 0 : onToggle(-1);
        onChange === null || onChange === void 0 ? void 0 : onChange(item.value, item);
      },
      className: (0, _classnames.default)(_indexModule.default.downSelect, _indexModule.default.flexCenter, props.itemCls, item.value === value && (0, _classnames.default)(props.itemSelectCls, _indexModule.default.select))
    }, item.text);
  })), children), /*#__PURE__*/_react.default.createElement(_one.View, {
    className: (0, _classnames.default)(_indexModule.default.downItem, _indexModule.default.flexCenter, props.className),
    style: new_style(),
    onTap: function onTap() {
      if (disabled) return;

      if (_onTap) {
        // 如果返回false，不响应展开折叠
        if (_onTap() !== false) {
          onToggle === null || onToggle === void 0 ? void 0 : onToggle();
        }
      } else {
        onToggle === null || onToggle === void 0 ? void 0 : onToggle();
      }
    }
  }, /*#__PURE__*/_react.default.createElement(_space.default, {
    size: 10,
    className: (0, _classnames.default)(_indexModule.default.flexCenter, titleCls)
  }, (selectItem === null || selectItem === void 0 ? void 0 : selectItem.text) || title, /*#__PURE__*/_react.default.createElement(_rotate.default, {
    run: showOptions,
    angle: 180
  }, /*#__PURE__*/_react.default.createElement(_icon.default, {
    size: arrowsSize,
    name: icon || 'down',
    color: arrowsColor,
    className: (0, _classnames.default)(_indexModule.default.icon, arrowsCls)
  })))));
};

exports.default = _default;