import { jsx as v } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/jsx-runtime/dist/jsxRuntime.module.js";
import { TRANSACTION_DATE_RANGE_CUSTOM as N, TRANSACTION_DATE_RANGE_DEFAULT as x, TRANSACTION_DATE_RANGE_MAX_YEARS as H, TRANSACTION_DATE_RANGES as E, TRANSACTION_DATE_RANGE_YEAR_TO_DATE as K, TRANSACTION_DATE_RANGE_LAST_MONTH as j, TRANSACTION_DATE_RANGE_THIS_MONTH as k, TRANSACTION_DATE_RANGE_LAST_WEEK as B, TRANSACTION_DATE_RANGE_THIS_WEEK as U, TRANSACTION_DATE_RANGE_LAST_180_DAYS as V, TRANSACTION_DATE_RANGE_LAST_30_DAYS as W, TRANSACTION_DATE_RANGE_LAST_7_DAYS as q } from "../../constants.js";
import { useMemo as T, useState as p, useRef as J, useCallback as d, useEffect as w } from "../../../../../external/.pnpm/preact@10.28.2/node_modules/preact/hooks/dist/hooks.module.js";
import { getDateRangeTimestamps as X } from "../../../../internal/Calendar/calendar/timerange/utils.js";
import { DateRangeFilterParam as h } from "../../../../internal/FilterBar/filters/DateFilter/types.js";
import Q from "../../../../internal/FilterBar/filters/DateFilter/DateFilterCore.js";
import Z from "../../../../../hooks/useAnalytics/useFilterAnalyticsEvent.js";
import $ from "../../../../../core/Context/preact/useCoreContext.js";
import z from "../../../../internal/Calendar/calendar/timerange/factory.js";
import { unreachable as ee } from "../../../../../utils/common.js";
import { EMPTY_OBJECT as te } from "../../../../../utils/value/constants.js";
const ne = (t) => {
  switch (t) {
    case N:
      return "Custom";
    case q:
      return "Last 7 days";
    case W:
      return "Last 30 days";
    case V:
      return "Last 180 days";
    case U:
      return "This week";
    case B:
      return "Last week";
    case k:
      return "This month";
    case j:
      return "Last month";
    case K:
      return "Year to date";
    default:
      return ee(t);
  }
}, me = ({ createdDate: t, eventCategory: G, eventSubCategory: L, setCreatedDate: D, timezone: l }) => {
  const { i18n: n } = $(), g = T(() => n.get("common.filters.types.date.label"), [n]), r = T(() => n.get(N), [n]), _ = T(() => n.get(x), [n]), [u, O] = p(_), [C, I] = p(!1), A = J(null), { from: m, to: R, since: y, until: F, now: b } = T(() => {
    const e = Date.now() + 1, a = new Date(e), s = new Date(a);
    s.setFullYear(s.getFullYear() - H);
    const { from: f, to: i } = X(t, e, l), S = new Date(f), P = new Date(i);
    return {
      from: S.toISOString(),
      to: P.toISOString(),
      since: s.toISOString(),
      until: a.toISOString(),
      now: e - 1
      // remove time shift
    };
  }, [t, l]), { logEvent: c } = Z({ category: G, subCategory: L, label: "Date filter" }), M = d(
    (o = te) => {
      const e = o.selectedPresetOption || _;
      if (e !== u || e === r) {
        let a = N, s;
        if (e === r) {
          const i = o[h.FROM], S = o[h.TO];
          s = z({
            from: new Date(i || m).getTime(),
            to: new Date(S || R).getTime()
          })();
        } else
          [a, s] = Object.entries(E).find(([i]) => n.get(i) === e);
        const f = e === r ? String([s.from, s.to]) : ne(a);
        O(e), D(s), c?.("update", f);
      }
    },
    [n, m, R, r, _, u, c, D]
  ), Y = d(() => {
    A.current = t, I(!0);
  }, [t]);
  return w(() => {
    C && A.current !== null && A.current !== t && (I(!1), A.current = null, c?.("reset"));
  }, [C, t, c]), w(() => {
    const o = Object.entries(E).find(([, e]) => e === t)?.[0];
    O(o ? n.get(o) : r);
  }, [t, r, n]), /* @__PURE__ */ v(
    Q,
    {
      name: "createdAt",
      now: b,
      label: g,
      "aria-label": g,
      sinceDate: y,
      untilDate: F,
      from: m,
      to: R,
      onChange: M,
      onResetAction: Y,
      selectedPresetOption: u,
      timeRangePresetOptions: E,
      timezone: l,
      showTimezoneInfo: !0
    }
  );
};
export {
  me as default
};
