import { SearchOutlined } from '@ant-design/icons';
import { ArrowChevronUp16SecondaryIcon, ArrowChevronUp16BlueIcon, ArrowChevronDown16SecondaryIcon, ArrowChevronDown16BlueIcon, XmarkCloseCircleFill16TertiaryIcon, XmarkCloseCircleFill16SecondaryIcon } from '@cloudtower/icons-react';
import { cx } from '@linaria/core';
import Icon from '../Icon/index.js';
import Input from '../Input/index.js';
import Tooltip from '../Tooltip/index.js';
import { Typo } from '../Typo/index.js';
import useParrotTranslation from '../../hooks/useParrotTranslation.js';
import { debounce } from 'lodash';
import React__default, { useState, useRef, useEffect, useCallback } from 'react';
import { useRecentSearch } from './SearchInput.hook.js';
import { CountTextStyles, IconContainerStyles, DisabledIconStyles, SearchInputStyles, SearchInputDropdownStyles } from './SearchInput.style.js';
import { Antd5Dropdown } from '../Antd5Dropdown/Antd5Dropdown.js';

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] = useState(0);
  const [value, setValue] = useState(props.value || "");
  const { t } = useParrotTranslation();
  const isNoMatch = total === 0;
  const [inputFocused, setInputFocused] = useState(false);
  const {
    recentSearchKeys,
    setRecentSearchKeysDebounceRef,
    antd5DropdownMenu
  } = useRecentSearch(
    recentSearchLocalStorageKey,
    maxRecentCount,
    setValue,
    onChange
  );
  const debounceOnChangeRef = useRef();
  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 = useCallback(
    (newCurrent) => {
      if (externalCurrent === void 0) {
        setInternalCurrent(newCurrent);
      }
    },
    [externalCurrent]
  );
  const next = 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 = 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.createElement(React__default.Fragment, null, typeof total === "number" ? /* @__PURE__ */ React__default.createElement(
    "span",
    {
      className: cx(
        Typo.Label.l4_regular,
        "counter-text",
        CountTextStyles
      )
    },
    current,
    "/",
    total
  ) : null, /* @__PURE__ */ React__default.createElement("span", { className: IconContainerStyles }, isNoMatch ? /* @__PURE__ */ React__default.createElement(
    Icon,
    {
      className: DisabledIconStyles,
      src: ArrowChevronUp16SecondaryIcon,
      style: { opacity: 0.5 }
    }
  ) : /* @__PURE__ */ React__default.createElement(Tooltip, { title: t("components.prev") }, /* @__PURE__ */ React__default.createElement(
    Icon,
    {
      src: prefixIcon != null ? prefixIcon : ArrowChevronUp16SecondaryIcon,
      hoverSrc: prefixHoverIcon != null ? prefixHoverIcon : ArrowChevronUp16BlueIcon,
      onClick: prev
    }
  )), isNoMatch ? /* @__PURE__ */ React__default.createElement(
    Icon,
    {
      className: DisabledIconStyles,
      src: ArrowChevronDown16SecondaryIcon,
      style: { opacity: 0.5 }
    }
  ) : /* @__PURE__ */ React__default.createElement(Tooltip, { title: t("components.next") }, /* @__PURE__ */ React__default.createElement(
    Icon,
    {
      src: nextIcon != null ? nextIcon : ArrowChevronDown16SecondaryIcon,
      hoverSrc: nextHoverIcon != null ? nextHoverIcon : ArrowChevronDown16BlueIcon,
      onClick: next
    }
  )), /* @__PURE__ */ React__default.createElement(Tooltip, { title: t("components.clear") }, /* @__PURE__ */ React__default.createElement(
    Icon,
    {
      src: clearIcon != null ? clearIcon : XmarkCloseCircleFill16TertiaryIcon,
      hoverSrc: clearHoverIcon != null ? clearHoverIcon : XmarkCloseCircleFill16SecondaryIcon,
      onClick: () => {
        var _a2;
        setValue("");
        (_a2 = debounceOnChangeRef.current) == null ? void 0 : _a2.call(debounceOnChangeRef, "");
      }
    }
  )))) : null;
  useEffect(() => {
    setValue(props.value || "");
  }, [props.value]);
  useEffect(() => {
    if (externalCurrent === void 0) {
      setCurrent(total ? 1 : 0);
    }
  }, [value, total, externalCurrent, setCurrent]);
  const InputComponent = /* @__PURE__ */ React__default.createElement(
    Input,
    __spreadValues({
      className: cx(SearchInputStyles, className),
      style: { width },
      prefix: searchIcon ? /* @__PURE__ */ React__default.createElement(Icon, { src: searchIcon }) : /* @__PURE__ */ React__default.createElement(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.createElement(
      Antd5Dropdown,
      {
        overlayClassName: cx(
          "recent-search-dropdown",
          SearchInputDropdownStyles
        ),
        overlayStyle: { width },
        transitionName: "",
        placement: "bottomLeft",
        trigger: ["click"],
        open: inputFocused && !value && !!recentSearchKeys.length,
        menu: antd5DropdownMenu
      },
      InputComponent
    );
  return InputComponent;
};

export { SearchInput };
