"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("react/jsx-runtime");
const React = require("react");
const Select$1 = require("../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Select/Select.cjs");
const MultiSelect$1 = require("../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/MultiSelect/MultiSelect.cjs");
const useUncontrolled = require("../../node_modules/.pnpm/@mantine_hooks@7.15.2_react@18.3.1/node_modules/@mantine/hooks/esm/use-uncontrolled/use-uncontrolled.cjs");
const getParsedComboboxData = require("../../node_modules/.pnpm/@mantine_core@7.15.2_patch_hash_jclkxeaefn6uz54h34k3r3yjsq_@mantine_hooks@7.15.2_react@18.3.1_szqfuioo5damkjdixckhzmwycq/node_modules/@mantine/core/esm/components/Combobox/get-parsed-combobox-data/get-parsed-combobox-data.cjs");
const defaultGetCreateLabel = (query) => `+ Create ${query}`;
const CREATE_VALUE_PREFIX = "$create:";
const defaultGetCreateValue = (query) => `${CREATE_VALUE_PREFIX}${query}`;
function useCreateableSelect(props) {
  const { creatable = false, getCreateLabel = defaultGetCreateLabel, onCreate, searchable, ...rest } = props;
  if (creatable && typeof onCreate !== "function") {
    throw new Error("`onCreate` is required when `creatable` is true");
  }
  const [value, setValue] = useUncontrolled.useUncontrolled({
    value: props.value,
    onChange: props.onChange,
    defaultValue: props.defaultValue
  });
  const [searchValue, setSearchValue] = useUncontrolled.useUncontrolled({
    value: props.searchValue,
    onChange: props.onSearchChange,
    defaultValue: ""
  });
  const parsedData = React.useMemo(() => {
    const parsedData2 = getParsedComboboxData.getParsedComboboxData(props.data);
    if (!creatable || !searchValue) {
      return parsedData2;
    }
    const placeholder = {
      label: getCreateLabel(searchValue),
      value: defaultGetCreateValue(searchValue)
    };
    if (parsedData2.findIndex((item) => {
      if ("value" in item) {
        return item.value === placeholder.value.slice(CREATE_VALUE_PREFIX.length);
      }
      return false;
    }) === -1) {
      parsedData2.push(placeholder);
    }
    return parsedData2;
  }, [props.data, searchValue]);
  const handleChange = (value2, option) => {
    const isMultiSelect = Array.isArray(value2);
    if (creatable) {
      const values = Array.isArray(value2) ? value2 : [value2];
      const clickedCreateItem = values.some((i) => typeof i === "string" && i.startsWith(CREATE_VALUE_PREFIX));
      if (clickedCreateItem) {
        const newItemValue = isMultiSelect ? values.find((i) => typeof i === "string" && i.startsWith(CREATE_VALUE_PREFIX)) : value2;
        if (newItemValue) {
          const createdItem = onCreate(newItemValue == null ? void 0 : newItemValue.slice(CREATE_VALUE_PREFIX.length));
          if (createdItem) {
            const nextValue = isMultiSelect ? [...values.filter((i) => !(i == null ? void 0 : i.startsWith(CREATE_VALUE_PREFIX))), createdItem.value] : createdItem.value;
            setValue(nextValue, isMultiSelect ? void 0 : option);
            setSearchValue("");
            return;
          }
        }
      }
    }
    setValue(value2, isMultiSelect ? void 0 : option);
  };
  return {
    ...rest,
    data: parsedData,
    value,
    onChange: handleChange,
    searchable: searchable || creatable,
    searchValue,
    onSearchChange: setSearchValue
  };
}
function Select(props) {
  const allProps = useCreateableSelect(props);
  return /* @__PURE__ */ jsxRuntime.jsx(Select$1.Select, { ...allProps, value: allProps.value || null });
}
function MultiSelect(props) {
  const allProps = useCreateableSelect(props);
  return /* @__PURE__ */ jsxRuntime.jsx(MultiSelect$1.MultiSelect, { ...allProps });
}
exports.MultiSelect = MultiSelect;
exports.Select = Select;
