"use strict";
const Toggle = require("./Toggle-CAW20E9V.cjs");
const jsxRuntime = require("./jsx-runtime-B3vWrP5x.cjs");
const css = require("./css-Pf9qxhQ4.cjs");
const aggregator = require("./aggregator-Ca8xiVD2.cjs");
const React = require("react");
const Center = require("./Center-5NJ61qfA.cjs");
const HelpText = ({
  className,
  variant = "error",
  ...props
}) => /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(
  Toggle.Text,
  {
    className: css.CSS(
      css.CSS.B("input-help-text"),
      css.CSS.BM("input-help-text", variant),
      className
    ),
    level: "small",
    ...props
  }
);
const Label = ({
  className,
  required = false,
  children,
  ...props
}) => {
  return /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsxs("label", { className: css.CSS(css.CSS.B("input-label"), className), ...props, children: [
    children,
    " ",
    required && /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx("span", { className: css.CSS.B("required-indicator"), children: "*" })
  ] });
};
const maybeDefaultAlignment = (align, dir = "x") => {
  if (align != null)
    return align;
  return css.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__ */ jsxRuntime.jsxRuntimeExports.jsx(jsxRuntime.jsxRuntimeExports.Fragment, { children });
  if (direction2 === "x")
    inputAndHelp = /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsxs(Center.Space, { direction: "y", size: "small", children: [
      children,
      (padHelpText || helpText != null && helpText.length > 0) && /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(HelpText, { variant: helpTextVariant, children: helpText })
    ] });
  else
    inputAndHelp = /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsxs(Center.Space, { direction: "y", size: 1 / 3, children: [
      children,
      (padHelpText || helpText != null && helpText.length > 0) && /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(HelpText, { variant: helpTextVariant, children: helpText })
    ] });
  return /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsxs(
    Center.Space,
    {
      className: css.CSS(css.CSS.B("input-item"), className),
      direction: direction2,
      size,
      align: maybeDefaultAlignment(align, direction2),
      ...props,
      children: [
        showLabel && /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(Label, { required, children: label }),
        inputAndHelp
      ]
    }
  );
};
const DateTime = ({
  value,
  onChange,
  onBlur,
  ...props
}) => {
  const ts = new aggregator.w(value, "UTC");
  const [internalValue, setInternalValue] = React.useState(
    ts.fString("ISO", "local").slice(0, -1)
  );
  const [valueIsValid, setValueIsValid] = React.useState(true);
  const handleChange = (next) => {
    let nextStr = next.toString();
    setInternalValue(nextStr);
    let ts2 = new aggregator.w(next, "UTC");
    if (nextStr.length < 23)
      nextStr += ".000";
    ts2 = ts2.add(
      BigInt(
        aggregator.w.now().date().getTimezoneOffset() - ts2.date().getTimezoneOffset()
      ) * aggregator.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 aggregator.w(value, "UTC").fString("ISO", "local").slice(0, -1));
    onBlur == null ? void 0 : onBlur(e);
  };
  return /* @__PURE__ */ jsxRuntime.jsxRuntimeExports.jsx(
    Toggle.Text$1,
    {
      type: "datetime-local",
      onBlur: handleBlur,
      required: false,
      value: valueIsValid ? new aggregator.w(value, "UTC").fString("ISO", "local").slice(0, -1) : internalValue,
      onChange: handleChange,
      ...props
    }
  );
};
const external = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
  __proto__: null,
  Date: Toggle.Date,
  DateTime,
  DragButton: Toggle.DragButton,
  HelpText,
  Item,
  Label,
  Numeric: Toggle.Numeric,
  Switch: Toggle.Switch$1,
  Text: Toggle.Text$1,
  TextArea: Toggle.TextArea,
  Time: Toggle.Time,
  combineDateAndTimeValue: Toggle.combineDateAndTimeValue,
  useDate: Toggle.useDate,
  useTime: Toggle.useTime
}, Symbol.toStringTag, { value: "Module" }));
exports.Item = Item;
exports.external = external;
//# sourceMappingURL=external-CZli69bF.cjs.map
