/* eslint-disable @typescript-eslint/no-explicit-any */
import React, { useMemo } from 'react';
import { View } from 'remax/one';
import classNames from 'classnames';
import styles from './styles/index.module.less';
import Icon from '../icon';
import Fold from '../fold';
import Rotate from '../rotate';
import Space from '../space';
import { useConfig } from '../config-provider';
export default (function (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 = useConfig(),
      brandPrimary = _useConfig.brandPrimary;

  var selectItem = 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.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Fold, {
    folded: !showOptions,
    className: styles.down,
    maxHeight: '50vh'
  }, /*#__PURE__*/React.createElement(Space, {
    vertical: true,
    style: {
      width: '100%'
    }
  }, options === null || options === void 0 ? void 0 : options.map(function (item) {
    return /*#__PURE__*/React.createElement(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: classNames(styles.downSelect, styles.flexCenter, props.itemCls, item.value === value && classNames(props.itemSelectCls, styles.select))
    }, item.text);
  })), children), /*#__PURE__*/React.createElement(View, {
    className: classNames(styles.downItem, styles.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.createElement(Space, {
    size: 10,
    className: classNames(styles.flexCenter, titleCls)
  }, (selectItem === null || selectItem === void 0 ? void 0 : selectItem.text) || title, /*#__PURE__*/React.createElement(Rotate, {
    run: showOptions,
    angle: 180
  }, /*#__PURE__*/React.createElement(Icon, {
    size: arrowsSize,
    name: icon || 'down',
    color: arrowsColor,
    className: classNames(styles.icon, arrowsCls)
  })))));
});