'use strict';

var icons = require('@ant-design/icons');
var iconsReact = require('@cloudtower/icons-react');
var core = require('@linaria/core');
var index$1 = require('../Icon/index.js');
var index$3 = require('../Input/index.js');
var index$2 = require('../Tooltip/index.js');
var index = require('../Typo/index.js');
var useParrotTranslation = require('../../hooks/useParrotTranslation.js');
var _ = require('lodash');
var React = require('react');
var SearchInput_hook = require('./SearchInput.hook.js');
var SearchInput_style = require('./SearchInput.style.js');
var Antd5Dropdown = require('../Antd5Dropdown/Antd5Dropdown.js');

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

var React__default = /*#__PURE__*/_interopDefault(React);

var __defProp = Object.defineProperty;
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 __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 SearchInput = (props) => {
  const _a = props, {
    className,
    debounceWait = 300,
    total = 0,
    onSearchNext,
    onSearchPrev,
    prefixHoverIcon,
    nextHoverIcon,
    clearHoverIcon,
    prefixIcon,
    nextIcon,
    clearIcon,
    width = 276,
    searchIcon,
    enableRecentSearch,
    recentSearchLocalStorageKey,
    maxRecentCount = 5,
    current: externalCurrent,
    onChange,
    onFocus,
    onBlur
  } = _a, restProps = __objRest(_a, [
    "className",
    "debounceWait",
    "total",
    "onSearchNext",
    "onSearchPrev",
    "prefixHoverIcon",
    "nextHoverIcon",
    "clearHoverIcon",
    "prefixIcon",
    "nextIcon",
    "clearIcon",
    "width",
    "searchIcon",
    "enableRecentSearch",
    "recentSearchLocalStorageKey",
    "maxRecentCount",
    "current",
    "onChange",
    "onFocus",
    "onBlur"
  ]);
  const [internalCurrent, setInternalCurrent] = React.useState(0);
  const [value, setValue] = React.useState(props.value || "");
  const { t } = useParrotTranslation();
  const isNoMatch = total === 0;
  const [inputFocused, setInputFocused] = React.useState(false);
  const {
    recentSearchKeys,
    setRecentSearchKeysDebounceRef,
    antd5DropdownMenu
  } = SearchInput_hook.useRecentSearch(
    recentSearchLocalStorageKey,
    maxRecentCount,
    setValue,
    onChange
  );
  const debounceOnChangeRef = React.useRef();
  React.useEffect(() => {
    const fn = _.debounce((val) => {
      var _a2;
      onChange == null ? void 0 : onChange(val);
      if (enableRecentSearch && val) {
        (_a2 = setRecentSearchKeysDebounceRef.current) == null ? void 0 : _a2.call(setRecentSearchKeysDebounceRef, val);
      }
    }, debounceWait);
    debounceOnChangeRef.current = fn;
    return () => fn.cancel();
  }, [
    onChange,
    enableRecentSearch,
    debounceWait,
    setRecentSearchKeysDebounceRef
  ]);
  const current = externalCurrent !== void 0 ? externalCurrent : internalCurrent;
  const setCurrent = React.useCallback(
    (newCurrent) => {
      if (externalCurrent === void 0) {
        setInternalCurrent(newCurrent);
      }
    },
    [externalCurrent]
  );
  const next = React.useCallback(() => {
    if (total) {
      const nextCurrent = current + 1 > total ? 1 : current + 1;
      onSearchNext == null ? void 0 : onSearchNext(value, nextCurrent);
      setCurrent(nextCurrent);
    }
  }, [onSearchNext, current, total, value, setCurrent]);
  const prev = React.useCallback(() => {
    if (total) {
      const prevCurrent = current - 1 < 1 ? total : current - 1;
      onSearchPrev == null ? void 0 : onSearchPrev(value, prevCurrent);
      setCurrent(prevCurrent);
    }
  }, [onSearchPrev, current, total, value, setCurrent]);
  const suffix = onSearchNext && onSearchPrev && value ? /* @__PURE__ */ React__default.default.createElement(React__default.default.Fragment, null, typeof total === "number" ? /* @__PURE__ */ React__default.default.createElement(
    "span",
    {
      className: core.cx(
        index.Typo.Label.l4_regular,
        "counter-text",
        SearchInput_style.CountTextStyles
      )
    },
    current,
    "/",
    total
  ) : null, /* @__PURE__ */ React__default.default.createElement("span", { className: SearchInput_style.IconContainerStyles }, isNoMatch ? /* @__PURE__ */ React__default.default.createElement(
    index$1,
    {
      className: SearchInput_style.DisabledIconStyles,
      src: iconsReact.ArrowChevronUp16SecondaryIcon,
      style: { opacity: 0.5 }
    }
  ) : /* @__PURE__ */ React__default.default.createElement(index$2, { title: t("components.prev") }, /* @__PURE__ */ React__default.default.createElement(
    index$1,
    {
      src: prefixIcon != null ? prefixIcon : iconsReact.ArrowChevronUp16SecondaryIcon,
      hoverSrc: prefixHoverIcon != null ? prefixHoverIcon : iconsReact.ArrowChevronUp16BlueIcon,
      onClick: prev
    }
  )), isNoMatch ? /* @__PURE__ */ React__default.default.createElement(
    index$1,
    {
      className: SearchInput_style.DisabledIconStyles,
      src: iconsReact.ArrowChevronDown16SecondaryIcon,
      style: { opacity: 0.5 }
    }
  ) : /* @__PURE__ */ React__default.default.createElement(index$2, { title: t("components.next") }, /* @__PURE__ */ React__default.default.createElement(
    index$1,
    {
      src: nextIcon != null ? nextIcon : iconsReact.ArrowChevronDown16SecondaryIcon,
      hoverSrc: nextHoverIcon != null ? nextHoverIcon : iconsReact.ArrowChevronDown16BlueIcon,
      onClick: next
    }
  )), /* @__PURE__ */ React__default.default.createElement(index$2, { title: t("components.clear") }, /* @__PURE__ */ React__default.default.createElement(
    index$1,
    {
      src: clearIcon != null ? clearIcon : iconsReact.XmarkCloseCircleFill16TertiaryIcon,
      hoverSrc: clearHoverIcon != null ? clearHoverIcon : iconsReact.XmarkCloseCircleFill16SecondaryIcon,
      onClick: () => {
        var _a2;
        setValue("");
        (_a2 = debounceOnChangeRef.current) == null ? void 0 : _a2.call(debounceOnChangeRef, "");
      }
    }
  )))) : null;
  React.useEffect(() => {
    setValue(props.value || "");
  }, [props.value]);
  React.useEffect(() => {
    if (externalCurrent === void 0) {
      setCurrent(total ? 1 : 0);
    }
  }, [value, total, externalCurrent, setCurrent]);
  const InputComponent = /* @__PURE__ */ React__default.default.createElement(
    index$3,
    __spreadValues({
      className: core.cx(SearchInput_style.SearchInputStyles, className),
      style: { width },
      prefix: searchIcon ? /* @__PURE__ */ React__default.default.createElement(index$1, { src: searchIcon }) : /* @__PURE__ */ React__default.default.createElement(icons.SearchOutlined, null),
      suffix,
      onPressEnter: next,
      value,
      onChange: (e) => {
        var _a2;
        const newValue = e.target.value || "";
        setValue(newValue);
        (_a2 = debounceOnChangeRef.current) == null ? void 0 : _a2.call(debounceOnChangeRef, newValue);
      },
      onFocus: (e) => {
        setInputFocused(true);
        onFocus == null ? void 0 : onFocus(e);
      },
      onBlur: (e) => {
        setInputFocused(false);
        onBlur == null ? void 0 : onBlur(e);
      }
    }, restProps)
  );
  if (enableRecentSearch)
    return /* @__PURE__ */ React__default.default.createElement(
      Antd5Dropdown.Antd5Dropdown,
      {
        overlayClassName: core.cx(
          "recent-search-dropdown",
          SearchInput_style.SearchInputDropdownStyles
        ),
        overlayStyle: { width },
        transitionName: "",
        placement: "bottomLeft",
        trigger: ["click"],
        open: inputFocused && !value && !!recentSearchKeys.length,
        menu: antd5DropdownMenu
      },
      InputComponent
    );
  return InputComponent;
};

exports.SearchInput = SearchInput;
