import Loading from '../Loading/index.js';
import { Typo } from '../Typo/index.js';
import { Select } from 'antd';
import cs from 'classnames';
import _ from 'lodash';
import React__default, { useMemo, useRef, useEffect } from 'react';
import { findDOMNode } from 'react-dom';
import { isElement } from 'react-is';
import { SelectStyle } from './select.style.js';

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: Typo.Label.l2_regular,
    middle: Typo.Label.l3_regular,
    small: Typo.Label.l4_regular
  }[size];
  const _danger = useMemo(() => {
    if (danger !== void 0) {
      return danger;
    }
    return (meta == null ? void 0 : meta.touched) && meta.invalid;
  }, [danger, meta]);
  const selectRef = useRef(null);
  useEffect(() => {
    if (!selectRef.current) {
      return;
    }
    const realDom = 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]);
  return /* @__PURE__ */ React__default.createElement(
    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(
        SelectStyle,
        "select",
        className,
        limitExceeded && "select-event-none",
        _danger ? "select-error" : "",
        typo
      ),
      "data-size": size,
      dropdownClassName: cs(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 && _.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.createElement(Loading, null) : notFoundContent,
      dropdownRender: (menu) => /* @__PURE__ */ React__default.createElement(React__default.Fragment, null, error || menu),
      loading,
      placeholder
    }), restProps),
    React__default.Children.map(children, (child) => {
      return isElement(child) ? __spreadProps(__spreadValues({}, child), {
        props: __spreadProps(__spreadValues({}, child.props), {
          "data-test": child.props.value
        })
      }) : child;
    })
  );
};
var LegacySelect$1 = LegacySelect;

export { LegacySelect$1 as default };
