"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const React = require("react");
const pick = require("../_util/pick.js");
const is = require("../_util/is.js");
const classNames = require("../_util/classNames.js");
const index = require("../ConfigProvider/index.js");
const index$3 = require("../../icon/react-icon/IconDown/index.js");
const index$1 = require("../../icon/react-icon/IconLoading/index.js");
const index$4 = require("../../icon/react-icon/IconClose/index.js");
const index$2 = require("../../icon/react-icon/IconSearch/index.js");
const inputTag = require("../InputTag/input-tag.js");
const inputElement = require("../Input/input-element.js");
const include = require("../_util/include.js");
const useForceUpdate = require("../_util/hooks/useForceUpdate.js");
const iconHover = require("./icon-hover.js");
const keycode = require("../_util/keycode.js");
const emotionReactJsxRuntime_browser_esm = require("../../../../../../@emotion_react@11.10.4_3toe27fv7etiytxb5kxc7fxaw4/node_modules/@emotion/react/jsx-runtime/dist/emotion-react-jsx-runtime.browser.esm.js");
const _interopDefaultLegacy = (e) => e && typeof e === "object" && "default" in e ? e : { default: e };
const React__default = /* @__PURE__ */ _interopDefaultLegacy(React);
var __assign = globalThis && globalThis.__assign || function() {
  __assign = Object.assign || function(t) {
    for (var s, i = 1, n = arguments.length; i < n; i++) {
      s = arguments[i];
      for (var p in s)
        if (Object.prototype.hasOwnProperty.call(s, p))
          t[p] = s[p];
    }
    return t;
  };
  return __assign.apply(this, arguments);
};
var __rest = globalThis && globalThis.__rest || function(s, e) {
  var t = {};
  for (var p in s)
    if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
      t[p] = s[p];
  if (s != null && typeof Object.getOwnPropertySymbols === "function")
    for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
      if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
        t[p[i]] = s[p[i]];
    }
  return t;
};
var __read = globalThis && globalThis.__read || function(o, n) {
  var m = typeof Symbol === "function" && o[Symbol.iterator];
  if (!m)
    return o;
  var i = m.call(o), r, ar = [], e;
  try {
    while ((n === void 0 || n-- > 0) && !(r = i.next()).done)
      ar.push(r.value);
  } catch (error) {
    e = {
      error
    };
  } finally {
    try {
      if (r && !r.done && (m = i["return"]))
        m.call(i);
    } finally {
      if (e)
        throw e.error;
    }
  }
  return ar;
};
var SearchStatus = {
  BEFORE: 0,
  EDITING: 1,
  NONE: 2
};
var SelectView = function(props, ref) {
  var _a, _b;
  var style = props.style, className = props.className, size = props.size, bordered = props.bordered, allowClear = props.allowClear, allowCreate = props.allowCreate, error = props.error, loading = props.loading, disabled = props.disabled, animation = props.animation, prefixCls = props.prefixCls, suffixIcon = props.suffixIcon, arrowIcon = props.arrowIcon, removeIcon = props.removeIcon, clearIcon = props.clearIcon, placeholder = props.placeholder, renderText = props.renderText, value = props.value, inputValue = props.inputValue, popupVisible = props.popupVisible, maxTagCount = props.maxTagCount, isMultiple = props.isMultiple, isEmptyValue = props.isEmptyValue, prefix = props.prefix, ariaControls = props.ariaControls, renderTag = props.renderTag, dragToSort = props.dragToSort, onKeyDown = props.onKeyDown, onChangeInputValue = props.onChangeInputValue, onPaste = props.onPaste, onClear = props.onClear, onFocus = props.onFocus, onBlur = props.onBlur, onRemoveCheckedItem = props.onRemoveCheckedItem, onSort = props.onSort, rtl = props.rtl, rest = __rest(props, ["style", "className", "size", "bordered", "allowClear", "allowCreate", "error", "loading", "disabled", "animation", "prefixCls", "suffixIcon", "arrowIcon", "removeIcon", "clearIcon", "placeholder", "renderText", "value", "inputValue", "popupVisible", "maxTagCount", "isMultiple", "isEmptyValue", "prefix", "ariaControls", "renderTag", "dragToSort", "onKeyDown", "onChangeInputValue", "onPaste", "onClear", "onFocus", "onBlur", "onRemoveCheckedItem", "onSort", "rtl"]);
  var refInput = React.useRef(null);
  var refWrapper = React.useRef(null);
  var _c = React.useContext(index.ConfigContext), ctxSize = _c.size, getPrefixCls = _c.getPrefixCls;
  var _d = __read(React.useState(SearchStatus.NONE), 2), searchStatus = _d[0], setSearchStatus = _d[1];
  var _e = __read(React.useState(false), 2), focused = _e[0], setFocused = _e[1];
  var forceUpdate = useForceUpdate();
  var showSearch = "showSearch" in props ? props.showSearch : isMultiple;
  var canFocusInput = showSearch || allowCreate;
  var mergedSize = size || ctxSize;
  var mergedFocused = focused || popupVisible;
  var isRetainInputValueSearch = is.isObject(showSearch) && showSearch.retainInputValue;
  var renderedValue = !isMultiple && value !== void 0 ? renderText(value).text : "";
  var keepFocus = function(event) {
    event && event.preventDefault();
  };
  var handleFocus = function(action) {
    var element = canFocusInput ? refInput.current : refWrapper.current;
    if (element) {
      action === "focus" ? element.focus() : element.blur();
    }
  };
  var tryTriggerFocusChange = function(action, event) {
    if (canFocusInput && event.target === refWrapper.current) {
      return;
    }
    if (action === "focus") {
      setFocused(true);
      onFocus && onFocus(event);
    } else {
      setFocused(false);
      onBlur && onBlur(event);
    }
  };
  var tryTriggerKeyDown = function(event) {
    if (canFocusInput && event.currentTarget === refWrapper.current) {
      return;
    }
    var keyCode = event.keyCode || event.which;
    if (keyCode === keycode.Enter.code) {
      event.preventDefault();
    }
    onKeyDown && onKeyDown(event);
  };
  React.useEffect(function() {
    handleFocus(popupVisible ? "focus" : "blur");
    if (canFocusInput) {
      setSearchStatus(popupVisible ? SearchStatus.BEFORE : SearchStatus.NONE);
    }
  }, [popupVisible]);
  React.useImperativeHandle(ref, function() {
    return {
      dom: refWrapper.current,
      focus: handleFocus.bind(null, "focus"),
      blur: handleFocus.bind(null, "blur"),
      getWidth: function() {
        return refWrapper.current && refWrapper.current.clientWidth;
      }
    };
  });
  var mergedArrowIcon = "arrowIcon" in props ? arrowIcon === null ? null : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
    className: prefixCls + "-arrow-icon",
    children: arrowIcon
  }) : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
    className: prefixCls + "-arrow-icon",
    children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$3, {})
  });
  var mergedSuffixIcon = loading ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("span", {
    className: prefixCls + "-loading-icon",
    children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$1, {})
  }) : suffixIcon ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("span", {
    className: prefixCls + "-suffix-icon",
    children: suffixIcon
  }) : props.showSearch && popupVisible ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
    className: prefixCls + "-search-icon",
    children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$2, {})
  }) : mergedArrowIcon;
  var inputEventHandlers = {
    paste: onPaste,
    keyDown: tryTriggerKeyDown,
    focus: function(event) {
      event.stopPropagation();
      tryTriggerFocusChange("focus", event);
    },
    blur: function(event) {
      event.stopPropagation();
      tryTriggerFocusChange("blur", event);
    },
    change: function(newValue, event) {
      setSearchStatus(SearchStatus.EDITING);
      onChangeInputValue && onChangeInputValue(newValue, event);
    }
  };
  var renderSingle = function() {
    var _a2, _b2;
    var _inputValue;
    switch (searchStatus) {
      case SearchStatus.BEFORE:
        _inputValue = inputValue || (isRetainInputValueSearch ? renderedValue : "");
        break;
      case SearchStatus.EDITING:
        _inputValue = inputValue || "";
        break;
      default:
        _inputValue = renderedValue;
        break;
    }
    var inputProps = {
      style: {
        width: "100%"
      },
      value: typeof _inputValue !== "object" ? _inputValue : "",
      placeholder: canFocusInput && renderedValue && typeof renderedValue !== "object" ? renderedValue : placeholder
    };
    if (canFocusInput) {
      inputProps.onPaste = inputEventHandlers.paste;
      inputProps.onKeyDown = inputEventHandlers.keyDown;
      inputProps.onFocus = inputEventHandlers.focus;
      inputProps.onBlur = inputEventHandlers.blur;
      inputProps.onChange = inputEventHandlers.change;
    } else {
      inputProps.tabIndex = -1;
      inputProps.style.pointerEvents = "none";
    }
    var needShowInput = !!(mergedFocused && canFocusInput || isEmptyValue);
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs(emotionReactJsxRuntime_browser_esm.Fragment, {
      children: [/* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(inputElement, {
        ...__assign({
          "aria-hidden": !needShowInput || void 0,
          ref: refInput,
          disabled,
          className: classNames(prefixCls + "-view-input", (_a2 = {}, _a2[prefixCls + "-hidden"] = !needShowInput, _a2)),
          autoComplete: "off"
        }, inputProps)
      }), /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("span", {
        "aria-hidden": needShowInput || void 0,
        className: classNames(prefixCls + "-view-value", (_b2 = {}, _b2[prefixCls + "-hidden"] = needShowInput, _b2)),
        children: _inputValue
      })]
    });
  };
  var renderMultiple = function() {
    var usedValue = is.isUndefined(value) ? [] : [].concat(value);
    var maxTagCountNumber = is.isObject(maxTagCount) ? maxTagCount.count : maxTagCount;
    var maxTagCountRender = is.isObject(maxTagCount) && is.isFunction(maxTagCount.render) ? maxTagCount.render : function(invisibleCount) {
      return "+" + invisibleCount + "...";
    };
    var usedMaxTagCount = typeof maxTagCountNumber === "number" ? Math.max(maxTagCountNumber, 0) : usedValue.length;
    var tagsToShow = [];
    var lastClosableTagIndex = -1;
    for (var i = usedValue.length - 1; i >= 0; i--) {
      var v = usedValue[i];
      var result = renderText(v);
      if (i < usedMaxTagCount) {
        tagsToShow.unshift({
          value: v,
          label: result.text,
          closable: !result.disabled
        });
      }
      if (!result.disabled && lastClosableTagIndex === -1) {
        lastClosableTagIndex = i;
      }
    }
    var invisibleTagCount = usedValue.length - usedMaxTagCount;
    if (invisibleTagCount > 0) {
      tagsToShow.push({
        label: maxTagCountRender(invisibleTagCount),
        closable: false,
        value: "__arco_value_tag_placeholder"
      });
    }
    var eventHandlers = {
      onPaste: inputEventHandlers.paste,
      onKeyDown: inputEventHandlers.keyDown,
      onFocus: inputEventHandlers.focus,
      onBlur: inputEventHandlers.blur,
      onInputChange: inputEventHandlers.change,
      onRemove: function(value2, index2, event) {
        var keyCode = event.keyCode || event.which;
        if (keyCode === keycode.Backspace.code && lastClosableTagIndex > -1) {
          value2 = usedValue[lastClosableTagIndex];
          index2 = lastClosableTagIndex;
        }
        maxTagCount && forceUpdate();
        onRemoveCheckedItem && onRemoveCheckedItem(value2, index2, event);
      }
    };
    return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(inputTag, {
      ...__assign({
        className: mergedFocused ? getPrefixCls("input-tag") + "-focus" : "",
        ref: refInput,
        disabled,
        dragToSort,
        disableInput: !showSearch,
        animation,
        placeholder,
        value: tagsToShow,
        inputValue,
        size: mergedSize,
        tagClassName: prefixCls + "-tag",
        renderTag,
        icon: {
          removeIcon
        },
        onChange: function(value2, reason) {
          if (onSort && reason === "sort") {
            onSort(value2);
          }
        }
      }, eventHandlers)
    });
  };
  var classNames$1 = classNames(prefixCls, prefixCls + "-" + (isMultiple ? "multiple" : "single"), (_a = {}, _a[prefixCls + "-show-search"] = showSearch, _a[prefixCls + "-open"] = popupVisible, _a[prefixCls + "-size-" + mergedSize] = mergedSize, _a[prefixCls + "-focused"] = mergedFocused, _a[prefixCls + "-error"] = error, _a[prefixCls + "-disabled"] = disabled, _a[prefixCls + "-no-border"] = !bordered, _a[prefixCls + "-rtl"] = rtl, _a), className);
  var mergedClearIcon = !disabled && !isEmptyValue && allowClear ? /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(iconHover, {
    size: mergedSize,
    className: prefixCls + "-clear-icon",
    onClick: onClear,
    onMouseDown: keepFocus,
    children: clearIcon !== void 0 && clearIcon !== null ? clearIcon : /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx(index$4, {})
  }, "clearIcon") : null;
  return /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
    ...__assign({
      role: "combobox",
      "aria-haspopup": "listbox",
      "aria-autocomplete": "list",
      "aria-expanded": popupVisible,
      "aria-disabled": disabled,
      "aria-controls": ariaControls
    }, include(rest, ["onClick", "onMouseEnter", "onMouseLeave"]), pick.pickDataAttributes(rest), {
      ref: refWrapper,
      tabIndex: disabled ? -1 : 0,
      style,
      className: classNames$1,
      onKeyDown: tryTriggerKeyDown,
      onFocus: function(event) {
        if (!disabled && !dragToSort) {
          if (canFocusInput) {
            refInput.current && refInput.current.focus();
          } else {
            tryTriggerFocusChange("focus", event);
          }
        }
      },
      onBlur: function(event) {
        return tryTriggerFocusChange("blur", event);
      }
    }),
    children: /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
      title: typeof renderedValue === "string" ? renderedValue : void 0,
      className: classNames(prefixCls + "-view", (_b = {}, _b[prefixCls + "-view-with-prefix"] = prefix, _b)),
      onClick: function(e) {
        return popupVisible && canFocusInput && e.stopPropagation();
      },
      children: [prefix && /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsx("div", {
        "aria-hidden": "true",
        className: classNames(prefixCls + "-prefix"),
        onMouseDown: function(event) {
          return focused && keepFocus(event);
        },
        children: prefix
      }), isMultiple ? renderMultiple() : renderSingle(), /* @__PURE__ */ emotionReactJsxRuntime_browser_esm.jsxs("div", {
        "aria-hidden": "true",
        className: prefixCls + "-suffix",
        onMouseDown: function(event) {
          return focused && keepFocus(event);
        },
        children: [mergedClearIcon, mergedSuffixIcon]
      })]
    })
  });
};
var SelectViewComponent = React__default.default.forwardRef(SelectView);
SelectViewComponent.displayName = "SelectView";
const SelectView$1 = SelectViewComponent;
exports.SelectView = SelectView;
exports.default = SelectView$1;
//# sourceMappingURL=select-view.js.map
