import { jsx as o } from "react/jsx-runtime";
import { classNames as _ } from "./index-classNames.js";
import { AnimatedConditional as M } from "./index-AnimatedConditional.js";
import { DateRange as O } from "./index-DateRange.js";
import { ensureIsRealDate as w } from "./index-date-range-picker-utils.js";
import { DateRangePickerNavigator as P } from "./index-DateRangePickerNavigator.js";
import { Text as V, TextTypes as F } from "./index-Text.js";
import { useState as y, useMemo as D, useEffect as T } from "react";
import { MOMENT_DAY_OF_WEEK_START as H } from "./index-moment.config.js";
const G = (m) => {
  const {
    condition: f,
    view: k,
    staticRanges: x,
    onChange: u,
    initialDate: s,
    onSelectionComplete: p,
    date: r,
    onDateClick: n
  } = m, [l, g] = y(0), {
    maxDate: R,
    minDate: S
  } = m, [i, h] = y({
    startDate: s == null ? void 0 : s.startDate,
    endDate: s == null ? void 0 : s.endDate
  }), d = D(() => {
    let e = i.startDate, t = i.endDate;
    const a = (c) => c instanceof Date && isNaN(c.getTime());
    return (a(e) || a(t)) && (e = /* @__PURE__ */ new Date(), t = /* @__PURE__ */ new Date()), {
      startDate: e,
      endDate: t
    };
  }, [i.startDate, i.endDate]), {
    startDate: v,
    endDate: b
  } = d, N = D(() => w(R), [R]), A = D(() => w(S), [S]);
  T(() => {
    r && r.startDate && r.endDate && h({
      startDate: r.startDate,
      endDate: r.endDate
    });
  }, [r]);
  const C = () => {
    p && p({
      selection: {
        startDate: v,
        endDate: b
      }
    }), g(0);
  };
  T(() => {
    l >= 2 && setTimeout(() => {
      C();
    }, 0);
  }, [l]);
  const E = (e) => {
    n && f && ((l === 0 ? "startDate" : "endDate") === "startDate" ? n && n(e, void 0, "single") : n && n(void 0, e, "single")), g((a) => a + 1);
  };
  return o(M, {
    animation: "none",
    condition: f,
    children: o(O, {
      navigatorRenderer: P,
      dayContentRenderer: (e) => o("span", {
        className: _("notranslate", "rdr-day-number-rc-btn"),
        onClick: () => {
          E(e);
        },
        style: {
          display: "block",
          width: 150
        },
        children: e.getDate()
      }),
      dragSelectionEnabled: !1,
      weekStartsOn: H,
      view: k,
      maxDate: N,
      minDate: A,
      inputRanges: [],
      initialValue: d,
      staticRanges: x,
      showDateDisplay: !1,
      calendarFocus: "backwards",
      preventSnapRefocus: !0,
      showPreview: !1,
      fixedHeight: !0,
      renderStaticRangeLabel: (e) => {
        const {
          startDate: t,
          endDate: a
        } = d, c = e.isSelected({
          endDate: a,
          startDate: t
        });
        return o("div", {
          onClick: () => C(),
          children: o(V, {
            replacements: e.replacements,
            color: c ? "--p500" : void 0,
            type: F.textHelp,
            children: e.label
          })
        });
      },
      onChange: (e) => {
        const {
          startDate: t,
          endDate: a
        } = e.selection;
        h({
          startDate: t,
          endDate: a
        }), n && n(t, a, "range"), u && u({
          selection: {
            startDate: t,
            endDate: a
          }
        });
      }
    })
  });
};
export {
  G as DateRangePickerBase
};
