'use strict';

var index$1 = require('../Loading/index.js');
var index = require('../Typo/index.js');
var useLegacyComponentWarning = require('../../hooks/useLegacyComponentWarning.js');
var antd = require('antd');
var cs = require('classnames');
var _ = require('lodash');
var React = require('react');
var reactDom = require('react-dom');
var reactIs = require('react-is');
var select_style = require('./select.style.js');

function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }

var cs__default = /*#__PURE__*/_interopDefault(cs);
var ___default = /*#__PURE__*/_interopDefault(_);
var React__default = /*#__PURE__*/_interopDefault(React);

var __defProp = Object.defineProperty;
var __defProps = Object.defineProperties;
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
const LegacySelect = (_a) => {
  var _b = _a, {
    input,
    multiple,
    className,
    scrollBottomBuffer = 0,
    onScrollBottom,
    onPopupScroll,
    onSearch,
    showSearch,
    filterOption,
    loading,
    notFoundContent,
    children,
    error,
    selectLimit,
    dropdownClassName,
    danger,
    size = "middle",
    meta,
    placeholder
  } = _b, restProps = __objRest(_b, [
    "input",
    "multiple",
    "className",
    "scrollBottomBuffer",
    "onScrollBottom",
    "onPopupScroll",
    "onSearch",
    "showSearch",
    "filterOption",
    "loading",
    "notFoundContent",
    "children",
    "error",
    "selectLimit",
    "dropdownClassName",
    "danger",
    "size",
    "meta",
    "placeholder"
  ]);
  var _a2;
  const limitExceeded = multiple && selectLimit && selectLimit <= (((_a2 = input.value) == null ? void 0 : _a2.length) || 0);
  const typo = {
    large: index.Typo.Label.l2_regular,
    middle: index.Typo.Label.l3_regular,
    small: index.Typo.Label.l4_regular
  }[size];
  const _danger = React.useMemo(() => {
    if (danger !== void 0) {
      return danger;
    }
    return (meta == null ? void 0 : meta.touched) && meta.invalid;
  }, [danger, meta]);
  const selectRef = React.useRef(null);
  React.useEffect(() => {
    if (!selectRef.current) {
      return;
    }
    const realDom = reactDom.findDOMNode(selectRef.current);
    if (realDom) {
      const inputDom = realDom.getElementsByClassName(
        "ant-select-selection-search-input"
      )[0];
      const item = realDom.getElementsByClassName(
        "ant-select-selection-item"
      )[0];
      inputDom && (placeholder || item) && inputDom.setAttribute(
        "data-test",
        String(placeholder || item.textContent)
      );
    }
  }, [selectRef, placeholder]);
  useLegacyComponentWarning(
    "Warning: LegacySelect is deprecated and will be removed in future versions. Please use Select instead."
  );
  return /* @__PURE__ */ React__default.default.createElement(
    antd.Select,
    __spreadValues(__spreadProps(__spreadValues({}, input), {
      ref: selectRef,
      size,
      value: multiple ? input.value || [] : input.value || void 0,
      onChange: (e, option) => {
        var _a3, _b2;
        if (Array.isArray(e) && e.some((v) => v === "")) {
          (_a3 = input.onChange) == null ? void 0 : _a3.call(input, [], option);
        } else {
          (_b2 = input.onChange) == null ? void 0 : _b2.call(input, e, option);
        }
      },
      onBlur: () => {
        var _a3;
        return (_a3 = input.onBlur) == null ? void 0 : _a3.call(input);
      },
      mode: multiple ? "multiple" : void 0,
      className: cs__default.default(
        select_style.SelectStyle,
        "select",
        className,
        limitExceeded && "select-event-none",
        _danger ? "select-error" : "",
        typo
      ),
      "data-size": size,
      dropdownClassName: cs__default.default(dropdownClassName, limitExceeded && "display-none"),
      showSearch: multiple ? void 0 : typeof showSearch === "undefined" ? Boolean(onSearch) : showSearch,
      filterOption: onSearch === void 0 ? filterOption === void 0 ? (input2, option) => {
        const label = option == null ? void 0 : option.label;
        if (!label || typeof label !== "string") {
          return false;
        }
        return label.toLowerCase().includes(input2.toLowerCase());
      } : filterOption : false,
      onSearch: onSearch && ___default.default.debounce(onSearch, 100),
      onPopupScroll: (e) => {
        onPopupScroll == null ? void 0 : onPopupScroll(e);
        const el = e.currentTarget;
        if (el.scrollHeight - el.offsetHeight - el.scrollTop <= scrollBottomBuffer) {
          onScrollBottom == null ? void 0 : onScrollBottom();
        }
      },
      notFoundContent: loading ? /* @__PURE__ */ React__default.default.createElement(index$1, null) : notFoundContent,
      dropdownRender: (menu) => /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, error || menu),
      loading,
      placeholder
    }), restProps),
    React__default.default.Children.map(children, (child) => {
      return reactIs.isElement(child) ? __spreadProps(__spreadValues({}, child), {
        props: __spreadProps(__spreadValues({}, child.props), {
          "data-test": child.props.value
        })
      }) : child;
    })
  );
};
var LegacySelect$1 = LegacySelect;

module.exports = LegacySelect$1;
