"use client";
import { jsx, jsxs } from "react/jsx-runtime";
import * as React from "react";
import { Plus } from "lucide-react";
import { cn } from "@open-mercato/shared/lib/utils";
import { Avatar } from "@open-mercato/ui/primitives/avatar";
import { DatePicker } from "@open-mercato/ui/primitives/date-picker";
import { IconButton } from "@open-mercato/ui/primitives/icon-button";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@open-mercato/ui/primitives/select";
import { Switch } from "@open-mercato/ui/primitives/switch";
const CONTROL_BORDER = "border border-input";
const CONTROL_TEXT = "text-sm text-foreground placeholder:text-muted-foreground";
const LABEL_CLASS = "text-xs font-medium text-muted-foreground";
const DROPDOWN_PANEL_CLASS = "absolute z-50 mt-1 max-h-56 w-full overflow-y-auto rounded-md border border-border bg-popover p-1 text-popover-foreground shadow-md";
function useDropdownDismiss(open, onClose) {
  const rootRef = React.useRef(null);
  React.useEffect(() => {
    if (!open) return;
    const handlePointerDown = (event) => {
      if (rootRef.current && event.target instanceof Node && !rootRef.current.contains(event.target)) onClose();
    };
    const handleKeyDown = (event) => {
      if (event.key === "Escape") {
        event.stopPropagation();
        onClose();
      }
    };
    document.addEventListener("pointerdown", handlePointerDown, true);
    document.addEventListener("keydown", handleKeyDown, true);
    return () => {
      document.removeEventListener("pointerdown", handlePointerDown, true);
      document.removeEventListener("keydown", handleKeyDown, true);
    };
  }, [open, onClose]);
  return rootRef;
}
function Field({ label, children, error, className }) {
  return /* @__PURE__ */ jsxs("div", { className: cn("flex w-full flex-col gap-1.5", className), children: [
    /* @__PURE__ */ jsx("span", { className: LABEL_CLASS, children: label }),
    children,
    error ? /* @__PURE__ */ jsx("p", { className: "text-xs text-status-error-text", children: error }) : null
  ] });
}
function UppercaseBadge({ style, className, children }) {
  return /* @__PURE__ */ jsx(
    "span",
    {
      className: cn(
        "inline-flex shrink-0 items-center justify-center rounded-full bg-muted px-2 py-0.5 text-overline font-medium uppercase text-muted-foreground",
        className
      ),
      style,
      children
    }
  );
}
function AllDayToggle({ checked, onCheckedChange, label }) {
  return /* @__PURE__ */ jsx(
    Switch,
    {
      checked,
      "aria-label": label,
      onCheckedChange,
      className: "h-6 w-10"
    }
  );
}
function parseDateValue(value) {
  if (!value) return null;
  const parsed = /* @__PURE__ */ new Date(`${value}T00:00:00`);
  return Number.isNaN(parsed.getTime()) ? null : parsed;
}
function formatDateValue(date) {
  return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(date.getDate()).padStart(2, "0")}`;
}
const EDITOR_SCROLL_EVENT = "om-calendar-editor-scroll";
function useCloseOnEditorScroll(setOpen) {
  React.useEffect(() => {
    const handler = () => setOpen(false);
    document.addEventListener(EDITOR_SCROLL_EVENT, handler);
    return () => document.removeEventListener(EDITOR_SCROLL_EVENT, handler);
  }, [setOpen]);
}
function DateControl({
  value,
  onChange,
  ariaLabel,
  className
}) {
  const [open, setOpen] = React.useState(false);
  useCloseOnEditorScroll(setOpen);
  return /* @__PURE__ */ jsx(
    DatePicker,
    {
      value: parseDateValue(value),
      onChange: (date) => {
        if (date) onChange(formatDateValue(date));
      },
      footer: "none",
      open,
      onOpenChange: setOpen,
      "aria-label": ariaLabel,
      className: cn("w-full", className)
    }
  );
}
const TIME_OPTIONS = Array.from({ length: 48 }, (_, index) => {
  const hour = Math.floor(index / 2);
  const minute = index % 2 === 0 ? "00" : "30";
  return `${String(hour).padStart(2, "0")}:${minute}`;
});
function TimeControl({ value, onChange, ariaLabel }) {
  const [open, setOpen] = React.useState(false);
  useCloseOnEditorScroll(setOpen);
  const options = value && !TIME_OPTIONS.includes(value) ? [value, ...TIME_OPTIONS] : TIME_OPTIONS;
  return /* @__PURE__ */ jsxs(Select, { value, onValueChange: onChange, open, onOpenChange: setOpen, children: [
    /* @__PURE__ */ jsx(SelectTrigger, { "aria-label": ariaLabel, className: "h-9 w-32 shrink-0", children: /* @__PURE__ */ jsx(SelectValue, {}) }),
    /* @__PURE__ */ jsx(SelectContent, { className: "max-h-64", children: options.map((time) => /* @__PURE__ */ jsx(SelectItem, { value: time, children: time }, time)) })
  ] });
}
function PersonChip({
  name,
  badge,
  compact,
  onRemove,
  removeLabel
}) {
  return /* @__PURE__ */ jsxs(
    "span",
    {
      className: cn(
        "inline-flex shrink-0 items-center gap-1.5 rounded-full bg-muted pl-1 pr-2",
        compact ? "py-0.5" : "py-1"
      ),
      children: [
        /* @__PURE__ */ jsx(Avatar, { size: "xs", label: name }),
        /* @__PURE__ */ jsx("span", { className: "max-w-40 truncate text-xs font-medium text-foreground", children: name }),
        badge,
        onRemove ? /* @__PURE__ */ jsx(
          IconButton,
          {
            variant: "ghost",
            size: "xs",
            onClick: (event) => {
              event.stopPropagation();
              onRemove();
            },
            "aria-label": removeLabel,
            className: "size-5 shrink-0",
            children: /* @__PURE__ */ jsx(Plus, { "aria-hidden": true, className: "size-3.5 rotate-45 opacity-50" })
          }
        ) : null
      ]
    }
  );
}
export {
  AllDayToggle,
  CONTROL_BORDER,
  CONTROL_TEXT,
  DROPDOWN_PANEL_CLASS,
  DateControl,
  EDITOR_SCROLL_EVENT,
  Field,
  LABEL_CLASS,
  PersonChip,
  TimeControl,
  UppercaseBadge,
  useDropdownDismiss
};
//# sourceMappingURL=inputs.js.map
