import { useState as l, useRef as L, useMemo as m, useCallback as b } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import C from "../../../Calendar/calendar/timerange/presets/yearToDate.js";
import k from "../../../Calendar/calendar/timerange/presets/lastMonth.js";
import K from "../../../Calendar/calendar/timerange/presets/thisMonth.js";
import z from "../../../Calendar/calendar/timerange/presets/lastWeek.js";
import x from "../../../Calendar/calendar/timerange/presets/thisWeek.js";
import _ from "../../../Calendar/calendar/timerange/presets/lastNDays.js";
import F from "../../../../../core/Context/preact/useCoreContext.js";
const o = Object.freeze({
  LAST_7_DAYS: "common.filters.types.date.rangeSelect.options.last7Days",
  LAST_30_DAYS: "common.filters.types.date.rangeSelect.options.last30Days",
  LAST_90_DAYS: "common.filters.types.date.rangeSelect.options.last90Days",
  THIS_WEEK: "common.filters.types.date.rangeSelect.options.thisWeek",
  LAST_WEEK: "common.filters.types.date.rangeSelect.options.lastWeek",
  THIS_MONTH: "common.filters.types.date.rangeSelect.options.thisMonth",
  LAST_MONTH: "common.filters.types.date.rangeSelect.options.lastMonth",
  YEAR_TO_DATE: "common.filters.types.date.rangeSelect.options.yearToDate"
}), V = ({
  exclude: s = []
} = {}) => {
  const e = {
    [o.LAST_7_DAYS]: _(7),
    [o.LAST_30_DAYS]: _(30),
    [o.LAST_90_DAYS]: _(90),
    [o.THIS_WEEK]: x(),
    [o.LAST_WEEK]: z(),
    [o.THIS_MONTH]: K(),
    [o.LAST_MONTH]: k(),
    [o.YEAR_TO_DATE]: C()
  };
  if (!s?.length) return Object.freeze(e);
  const i = new Set(s);
  return Object.freeze(
    Object.fromEntries(Object.entries(e).filter(([a]) => !i.has(a)))
  );
}, X = ({
  now: s,
  options: e,
  selectedOption: i,
  timezone: a
}) => {
  const { i18n: S } = F(), [I, u] = l(), [M, O] = l(), [A, f] = l(), g = L(), E = L(), [n, y, r] = m(() => {
    const c = S.get("common.filters.types.date.rangeSelect.options.custom"), t = Object.keys(e), R = Object.freeze(t.map((T) => S.get(T)));
    return [c, (T, N = R) => {
      const Y = N.findIndex((H) => H === T);
      return e[t[Y]];
    }, R];
  }, [S, e]), [d, D] = l(i === n), h = m(() => Object.freeze([...r, n]), [n, r]), W = m(
    () => d ? h : r,
    [d, r, h]
  ), p = b(
    (c) => {
      const t = y(c, r);
      t && (u(t.from), O(t.to), D(!1), f(c));
    },
    [y, r]
  ), j = b(() => {
    u(void 0), O(void 0), D(!0), f(n);
  }, [n]);
  return m(() => {
    i === n ? f(n) : p(i);
  }, []), m(() => {
    if (g.current !== s || E.current !== a) {
      const c = Object.values(e);
      c.forEach((t) => {
        t.now = s ?? null, t.timezone = a;
      }), g.current = s, E.current = c[0]?.timezone, p(A);
    }
  }, [s, a, e]), {
    customSelection: j,
    from: I,
    onSelection: p,
    options: W,
    selectedOption: A,
    to: M
  };
};
export {
  o as TIME_RANGE_SELECTION_PRESET_OPTION_KEYS,
  V as getTimeRangeSelectionDefaultPresetOptions,
  X as useTimeRangeSelection
};
