import { e as Text, ah as Text$1, ai as Date, aj as DragButton, ak as Numeric, al as Switch, am as TextArea, an as Time, ao as combineDateAndTimeValue, ap as useDate, aq as useTime } from "./Toggle-DcOiWiHG.js";
import { j as jsxRuntimeExports } from "./jsx-runtime-mMz73N64.js";
import { C as CSS, d as f } from "./css-DoalWJF6.js";
import { w, y } from "./aggregator-Dl2jHye5.js";
import { useState } from "react";
import { S as Space } from "./Center-hbwH7rg9.js";
const HelpText = ({
  className,
  variant = "error",
  ...props
}) => /* @__PURE__ */ jsxRuntimeExports.jsx(
  Text,
  {
    className: CSS(
      CSS.B("input-help-text"),
      CSS.BM("input-help-text", variant),
      className
    ),
    level: "small",
    ...props
  }
);
const Label = ({
  className,
  required = false,
  children,
  ...props
}) => {
  return /* @__PURE__ */ jsxRuntimeExports.jsxs("label", { className: CSS(CSS.B("input-label"), className), ...props, children: [
    children,
    " ",
    required && /* @__PURE__ */ jsxRuntimeExports.jsx("span", { className: CSS.B("required-indicator"), children: "*" })
  ] });
};
const maybeDefaultAlignment = (align, dir = "x") => {
  if (align != null)
    return align;
  return f.construct(dir) === "y" ? "stretch" : "center";
};
const Item = ({
  label,
  showLabel = true,
  helpText,
  direction: direction2 = "y",
  className,
  children,
  required,
  align,
  size = "small",
  padHelpText = false,
  helpTextVariant,
  showHelpText = true,
  ...props
}) => {
  let inputAndHelp;
  if (showHelpText === false && showLabel === false)
    return /* @__PURE__ */ jsxRuntimeExports.jsx(jsxRuntimeExports.Fragment, { children });
  if (direction2 === "x")
    inputAndHelp = /* @__PURE__ */ jsxRuntimeExports.jsxs(Space, { direction: "y", size: "small", children: [
      children,
      (padHelpText || helpText != null && helpText.length > 0) && /* @__PURE__ */ jsxRuntimeExports.jsx(HelpText, { variant: helpTextVariant, children: helpText })
    ] });
  else
    inputAndHelp = /* @__PURE__ */ jsxRuntimeExports.jsxs(Space, { direction: "y", size: 1 / 3, children: [
      children,
      (padHelpText || helpText != null && helpText.length > 0) && /* @__PURE__ */ jsxRuntimeExports.jsx(HelpText, { variant: helpTextVariant, children: helpText })
    ] });
  return /* @__PURE__ */ jsxRuntimeExports.jsxs(
    Space,
    {
      className: CSS(CSS.B("input-item"), className),
      direction: direction2,
      size,
      align: maybeDefaultAlignment(align, direction2),
      ...props,
      children: [
        showLabel && /* @__PURE__ */ jsxRuntimeExports.jsx(Label, { required, children: label }),
        inputAndHelp
      ]
    }
  );
};
const DateTime = ({
  value,
  onChange,
  onBlur,
  ...props
}) => {
  const ts = new w(value, "UTC");
  const [internalValue, setInternalValue] = useState(
    ts.fString("ISO", "local").slice(0, -1)
  );
  const [valueIsValid, setValueIsValid] = useState(true);
  const handleChange = (next) => {
    let nextStr = next.toString();
    setInternalValue(nextStr);
    let ts2 = new w(next, "UTC");
    if (nextStr.length < 23)
      nextStr += ".000";
    ts2 = ts2.add(
      BigInt(
        w.now().date().getTimezoneOffset() - ts2.date().getTimezoneOffset()
      ) * y.MINUTE.valueOf()
    );
    let ok = false;
    try {
      const str = ts2.fString("ISO", "local");
      ok = str.slice(0, -1) === nextStr;
    } catch (_) {
      console.error("e");
    }
    if (!ok) {
      setValueIsValid(false);
      return;
    }
    onChange(Number(ts2.valueOf()));
    setValueIsValid(true);
  };
  const handleBlur = (e) => {
    setValueIsValid(true);
    setInternalValue(new w(value, "UTC").fString("ISO", "local").slice(0, -1));
    onBlur == null ? void 0 : onBlur(e);
  };
  return /* @__PURE__ */ jsxRuntimeExports.jsx(
    Text$1,
    {
      type: "datetime-local",
      onBlur: handleBlur,
      required: false,
      value: valueIsValid ? new w(value, "UTC").fString("ISO", "local").slice(0, -1) : internalValue,
      onChange: handleChange,
      ...props
    }
  );
};
const external = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  Date,
  DateTime,
  DragButton,
  HelpText,
  Item,
  Label,
  Numeric,
  Switch,
  Text: Text$1,
  TextArea,
  Time,
  combineDateAndTimeValue,
  useDate,
  useTime
}, Symbol.toStringTag, { value: "Module" }));
export {
  Item as I,
  external as e
};
//# sourceMappingURL=external-Brr6przd.js.map
