'use strict';

var jsxRuntime = require('react/jsx-runtime');
var React = require('react');
var utils = require('./utils-BhM0B89p.js');
var primitives_Field = require('./primitives/Field.js');
var x = require('./x-Brw3FJst.js');

const EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9-]+(?:\.[A-Z0-9-]+)*\.[A-Z]{2,}$/i;
const UNSTRICT_EMAIL_REGEX = /(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/g;
const EMAIL_SEPARATION_REGEX = /[^\s,]+/g;

const formatEmailInputOption = (email) => ({
  label: email,
  value: email,
  valid: EMAIL_REGEX.test(email)
});
const pruneDuplicates = (existing, newEmailStrings) => {
  const seen = new Set(existing.map((e) => e.value.toLowerCase()));
  const duplicates = [];
  const added = [];
  for (const emailStr of newEmailStrings) {
    const lower = emailStr.toLowerCase();
    if (seen.has(lower)) {
      duplicates.push(emailStr);
    } else {
      seen.add(lower);
      added.push(formatEmailInputOption(emailStr));
    }
  }
  return { uniqueEmails: [...existing, ...added], duplicates };
};
const filterValidEmails = (emails) => emails.filter((email) => email.valid);
const pluralizeEmail = (count) => count === 1 ? "email" : "emails";
const formatDuplicateWarning = (duplicates) => {
  const count = duplicates.length;
  return `Removed ${count} duplicate ${pluralizeEmail(count)} from the list: ${duplicates.join(", ")}`;
};

function useMultiEmailState(props) {
  const {
    value = [],
    onChange,
    onBlur,
    visibleEmailsCount = 3,
    isAlwaysExpanded = false,
    counter,
    filterInvalidEmails
  } = props;
  const [inputValue, setInputValue] = React.useState("");
  const [isFocused, setIsFocused] = React.useState(false);
  const [duplicateEmails, setDuplicateEmails] = React.useState([]);
  const addEmails = React.useCallback(
    (input) => {
      const trimmed = input.trim();
      if (!trimmed) return;
      UNSTRICT_EMAIL_REGEX.lastIndex = 0;
      EMAIL_SEPARATION_REGEX.lastIndex = 0;
      const emailMatches = trimmed.match(UNSTRICT_EMAIL_REGEX) || trimmed.match(EMAIL_SEPARATION_REGEX) || [];
      if (emailMatches.length === 0) return;
      const { uniqueEmails, duplicates } = pruneDuplicates(value, emailMatches);
      onChange?.(uniqueEmails);
      setDuplicateEmails(duplicates);
      setInputValue("");
    },
    [value, onChange]
  );
  const handleKeyDown = React.useCallback(
    (event) => {
      if (!inputValue && event.key === "Backspace" && value.length > 0) {
        onChange?.(value.slice(0, -1));
        return;
      }
      if (!inputValue) return;
      if (["Enter", "Tab", ",", " "].includes(event.key)) {
        addEmails(inputValue);
        event.preventDefault();
        event.stopPropagation();
      }
    },
    [inputValue, value, onChange, addEmails]
  );
  const handlePaste = React.useCallback(
    (event) => {
      const text = event.clipboardData.getData("text");
      if (!text) return;
      event.preventDefault();
      addEmails(text);
    },
    [addEmails]
  );
  const handleBlur = React.useCallback(() => {
    if (inputValue) {
      addEmails(inputValue);
    } else {
      onBlur?.();
    }
    setIsFocused(false);
    setDuplicateEmails([]);
  }, [inputValue, addEmails, onBlur]);
  const handleFocus = React.useCallback(() => setIsFocused(true), []);
  const handleChipRemove = React.useCallback(
    (emailValue) => onChange?.(value.filter((e) => e.value !== emailValue)),
    [value, onChange]
  );
  const handleFilterInvalidEmails = React.useCallback(
    () => onChange?.(filterValidEmails(value)),
    [value, onChange]
  );
  const validCount = React.useMemo(() => value.filter((e) => e.valid).length, [value]);
  const isCounterVisible = !!counter && (typeof counter === "boolean" || !counter.startsFrom || validCount >= counter.startsFrom);
  const shouldCollapse = !isAlwaysExpanded && !isFocused && value.length > visibleEmailsCount;
  const visibleEmails = shouldCollapse ? value.slice(0, visibleEmailsCount) : value;
  const hiddenCount = shouldCollapse ? value.length - visibleEmailsCount : 0;
  const isFilterLinkVisible = !!filterInvalidEmails && value.length > validCount;
  return {
    inputValue,
    setInputValue,
    isFocused,
    duplicateEmails,
    handleKeyDown,
    handlePaste,
    handleBlur,
    handleFocus,
    handleChipRemove,
    handleFilterInvalidEmails,
    isCounterVisible,
    validCount,
    visibleEmails,
    hiddenCount,
    isFilterLinkVisible
  };
}

const CHIP_BASE = "flex h-[calc(--spacing(5.25))] w-fit items-center justify-center gap-1 rounded-sm bg-muted px-1.5 text-xs font-medium whitespace-nowrap text-foreground";
const CHIP_INVALID = "border border-destructive text-destructive bg-destructive/10";
const CHIPS_CONTAINER = "flex min-h-8 flex-wrap items-center gap-1 rounded-lg border border-input bg-transparent bg-clip-padding px-2.5 py-1 text-sm transition-colors focus-within:border-ring focus-within:ring-3 focus-within:ring-ring/50";
const CHIPS_ERROR = "border-destructive ring-3 ring-destructive/20 dark:border-destructive/50 dark:ring-destructive/40";
const MultiEmailInput = React.forwardRef((props, ref) => {
  const {
    label,
    placeholder = "",
    helpText,
    error,
    disabled = false,
    required = false,
    maxHeight = 200,
    counter,
    filterInvalidEmails,
    prefix,
    suffix,
    labelProps,
    className
  } = props;
  const id = React.useId();
  const {
    isCounterVisible,
    validCount,
    visibleEmails,
    hiddenCount,
    inputValue,
    setInputValue,
    handleKeyDown,
    handlePaste,
    handleFocus,
    handleBlur,
    handleChipRemove,
    isFilterLinkVisible,
    handleFilterInvalidEmails,
    duplicateEmails
  } = useMultiEmailState(props);
  return /* @__PURE__ */ jsxRuntime.jsxs(primitives_Field.Field, { className: utils.cn("flex flex-col gap-1.5", className), children: [
    (label || isCounterVisible) && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-between", children: [
      label && /* @__PURE__ */ jsxRuntime.jsxs(primitives_Field.FieldLabel, { htmlFor: id, ...labelProps, children: [
        label,
        required && /* @__PURE__ */ jsxRuntime.jsx("span", { "aria-hidden": "true", className: "text-destructive", children: " *" })
      ] }),
      isCounterVisible && /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "text-xs text-muted-foreground", children: [
        validCount,
        " ",
        typeof counter === "object" && counter?.label ? counter.label : pluralizeEmail(validCount)
      ] })
    ] }),
    /* @__PURE__ */ jsxRuntime.jsxs(
      "div",
      {
        className: utils.cn(
          CHIPS_CONTAINER,
          !!error && CHIPS_ERROR,
          disabled && "pointer-events-none opacity-50"
        ),
        style: { maxHeight: `${maxHeight}px`, overflowY: "auto" },
        children: [
          prefix && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "shrink-0", children: prefix }),
          visibleEmails.map((email) => /* @__PURE__ */ jsxRuntime.jsxs(
            "span",
            {
              className: utils.cn(CHIP_BASE, !email.valid && CHIP_INVALID),
              children: [
                email.label,
                !disabled && /* @__PURE__ */ jsxRuntime.jsx(
                  "button",
                  {
                    type: "button",
                    tabIndex: -1,
                    className: "-ms-0.5 rounded-xs opacity-50 hover:opacity-100",
                    onMouseDown: (e) => e.preventDefault(),
                    onClick: () => handleChipRemove(email.value),
                    "aria-label": `Remove ${email.label}`,
                    children: /* @__PURE__ */ jsxRuntime.jsx(x.X, { className: "size-3 pointer-events-none" })
                  }
                )
              ]
            },
            email.value
          )),
          hiddenCount > 0 && /* @__PURE__ */ jsxRuntime.jsxs("span", { className: "flex h-[calc(--spacing(5.25))] items-center rounded-sm bg-secondary px-1.5 text-xs font-medium text-secondary-foreground", children: [
            "+",
            hiddenCount,
            " more"
          ] }),
          /* @__PURE__ */ jsxRuntime.jsx(
            "input",
            {
              ref,
              id,
              type: "text",
              value: inputValue,
              onChange: (e) => setInputValue(e.target.value),
              onKeyDown: handleKeyDown,
              onPaste: handlePaste,
              onFocus: handleFocus,
              onBlur: handleBlur,
              placeholder: props.value?.length ? "" : placeholder,
              disabled,
              className: "min-w-16 flex-1 bg-transparent text-sm outline-none placeholder:text-muted-foreground"
            }
          ),
          suffix && /* @__PURE__ */ jsxRuntime.jsx("div", { className: "shrink-0", children: suffix })
        ]
      }
    ),
    /* @__PURE__ */ jsxRuntime.jsxs(primitives_Field.FieldContent, { children: [
      !!error && /* @__PURE__ */ jsxRuntime.jsxs("p", { className: "text-[0.8rem] text-destructive", children: [
        error,
        isFilterLinkVisible && /* @__PURE__ */ jsxRuntime.jsx(
          "button",
          {
            type: "button",
            className: "ml-1 cursor-pointer font-semibold underline",
            onClick: handleFilterInvalidEmails,
            children: filterInvalidEmails?.label ?? "Click here to remove invalid emails."
          }
        )
      ] }),
      !!helpText && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-[0.8rem] text-muted-foreground", children: helpText }),
      duplicateEmails.length > 0 && /* @__PURE__ */ jsxRuntime.jsx("p", { className: "text-[0.8rem] text-amber-600 dark:text-amber-400", children: formatDuplicateWarning(duplicateEmails) })
    ] })
  ] });
});
MultiEmailInput.displayName = "MultiEmailInput";

exports.MultiEmailInput = MultiEmailInput;
//# sourceMappingURL=MultiEmailInput-CPXODFvs.js.map
