import { jsx as h } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import A from "../../../../../hooks/useTimezoneAwareDateFormatting.js";
import { useState as S, useCallback as v, useMemo as C, useEffect as P, useRef as k } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { BASE_LOCALE as x } from "../../../../../core/Localization/datetime/restamper/constants.js";
import { DateRangeFilterParam as R } from "./types.js";
/* empty css                    */
import B from "../../../../../core/Context/preact/useCoreContext.js";
import z from "../BaseFilter/BaseFilter.js";
import I from "../../../DatePicker/DatePicker.js";
import { EMPTY_OBJECT as V } from "../../../../../utils/value/constants.js";
import { CommitAction as E } from "../../../../../hooks/useCommitAction/types.js";
const _ = {
  month: "short",
  day: "numeric",
  year: "numeric"
}, L = (n, e, o, r) => {
  const m = o && e(o), t = r && e(r);
  if (m && t)
    return n.get("common.filters.types.date.range.between", { values: { fromDate: m, toDate: t } });
  if (m)
    return n.get("common.filters.types.date.range.since", { values: { date: m } });
  if (t)
    return n.get("common.filters.types.date.range.until", { values: { date: t } });
}, s = (n) => {
  try {
    return new Date(n || "").toISOString();
  } catch {
    return "";
  }
}, Y = /* @__PURE__ */ (() => {
  const n = ({
    editAction: e,
    from: o,
    to: r,
    now: m,
    onChange: t,
    onValueUpdated: i,
    showTimezoneInfo: T,
    selectedPresetOption: f,
    timeRangePresetOptions: y,
    timeRangeSelectorLabel: c,
    timezone: g,
    sinceDate: l,
    untilDate: F
  }) => {
    const { i18n: w } = B(), { fullDateFormat: D } = A(g), [d, b] = S(f), u = C(() => [new Date(o), new Date(r)], [o, r]), a = k(), p = v(
      (O, M) => {
        i(L(w, D, s(O), s(M)));
      },
      [w, D, i]
    );
    return P(() => {
      switch (e) {
        case E.APPLY:
          t({
            selectedPresetOption: d,
            [R.FROM]: s(a.current?.from),
            [R.TO]: s(a.current?.to)
          });
          break;
        case E.CLEAR:
          a.current?.clear(), t();
      }
    }, [e, t, d]), /* @__PURE__ */ h(
      I,
      {
        ref: a,
        now: m,
        originDate: u,
        onHighlight: p,
        onPresetOptionSelected: b,
        selectedPresetOption: f,
        timeRangePresetOptions: y,
        timeRangeSelectorLabel: c,
        timezone: g,
        showTimezoneInfo: T,
        sinceDate: s(l),
        untilDate: s(F)
      }
    );
  };
  return (e) => /* @__PURE__ */ h(n, { ...e });
})(), j = (n, e, o) => n.formatRange(e, o);
function U({
  title: n,
  from: e,
  to: o,
  selectedPresetOption: r,
  timeRangeSelectorLabel: m,
  ...t
}) {
  const { i18n: i } = B(), { fullDateFormat: T } = A(t.timezone), [f, y] = S(), [c, g] = S(), [l, F] = S(), w = v(
    (u) => {
      const { from: a, to: p, selectedPresetOption: O } = u ?? V;
      try {
        y(f ?? O), g(s(c ?? a)), F(s(l ?? p));
      } finally {
        t.onChange({ from: a, to: p, selectedPresetOption: O });
      }
    },
    [f, c, l, t]
  ), D = C(() => i.get("common.filters.types.date.rangeSelect.options.custom"), [i]), d = C(() => {
    const u = { ..._, timeZone: t.timezone };
    let a = new Intl.DateTimeFormat(x, u);
    try {
      a = new Intl.DateTimeFormat(i.locale, u);
    } catch {
    }
    return a;
  }, [i, t.timezone]);
  P(() => y(r), [r]), P(() => g(s(e || Date.now())), [e]), P(() => F(s(o || Date.now())), [o]);
  const b = C(() => r === D && c && l ? j(d, new Date(c), new Date(l)) : r ?? t.label, [D, d, c, l, r, t.label]);
  return /* @__PURE__ */ h(
    z,
    {
      ...t,
      from: e,
      to: o,
      type: "date",
      label: b,
      onChange: w,
      render: Y,
      selectedPresetOption: r,
      timeRangeSelectorLabel: m ?? i.get("common.filters.types.date.rangeSelect.label"),
      value: L(i, T, e, o),
      withContentPadding: !1
    }
  );
}
export {
  U as default
};
