import { jsxs as I, jsx as n, Fragment as F } from "react/jsx-runtime";
import { Box as a, Tooltip as M } from "@mui/material";
import { useEffect as R, useMemo as $ } from "react";
import s from "dayjs";
import "dayjs/locale/zh-cn";
import k from "dayjs/plugin/utc";
import B from "dayjs/plugin/timezone";
import E from "dayjs/plugin/relativeTime";
import H from "dayjs/plugin/updateLocale";
import N from "dayjs/plugin/localizedFormat";
import { create as Z } from "zustand";
import { formatToDatetime as O, setDateTool as q } from "../Util/index.js";
s.extend(N);
s.extend(k);
s.extend(B);
s.extend(H);
s.extend(E);
s.updateLocale("zh-cn", {
  // copy with https://github.com/iamkun/dayjs/blob/dev/src/locale/zh-cn.js
  relativeTime: {
    future: "%s后",
    past: "%s前",
    s: "几秒",
    m: "1 分钟",
    mm: "%d 分钟",
    h: "1 小时",
    hh: "%d 小时",
    d: "1 天",
    dd: "%d 天",
    M: "1 个月",
    MM: "%d 个月",
    y: "1 年",
    yy: "%d 年"
  }
});
q(s);
const j = {
  en: {
    utc: "UTC Timezone",
    local: "Local Timezone",
    shortUTC: "UTC",
    shortLocal: "Local"
  },
  zh: {
    utc: "UTC 时区",
    local: "当前时区",
    shortUTC: "UTC",
    shortLocal: "当前时区"
  }
}, A = Z((t) => ({
  isUtc: !1,
  setIsUtc: (o) => t({ isUtc: o })
}));
function w({
  value: t,
  locale: o = "en",
  withoutSuffix: p = !1,
  from: d = "",
  to: x = "",
  type: i = "relative",
  tz: l,
  relativeRange: m,
  format: g
}) {
  const C = (/* @__PURE__ */ new Date()).getTimezoneOffset(), T = C > 0 ? "-" : "+", v = Math.abs(C) / 60, L = C === 0, r = A((y) => y.isUtc), U = A((y) => y.setIsUtc);
  if (R(() => {
    U(L);
  }, [L]), !t)
    return { innerContent: "-", popContent: "-", isUtc: r, setIsUtc: U, sign: T, hoursOffset: v };
  const h = o === "zh" ? "zh-cn" : "en-us";
  let c = s(t).locale(h);
  i === "utc" && (c = s(t).utc().locale(h)), l && (c = c.tz(l));
  const f = O(t, { locale: h, tz: l, format: g });
  let e;
  if (d)
    e = c.from(d, p);
  else if (x)
    e = c.to(x, p);
  else if (m) {
    const y = c.diff(s());
    Math.abs(y) > m && (e = f);
  }
  e || (e = c.fromNow(p));
  let u = e, b = f;
  return i === "absolute" && (u = f, b = e), i === "utc" && (r ? (u = O(t, { locale: h, tz: l, isUtc: !0, format: g }), b = O(t, { locale: h, tz: l, isUtc: !0, format: g })) : (u = f, b = e)), { innerContent: u, popContent: b, isUtc: r, setIsUtc: U, sign: T, hoursOffset: v, relativeString: e, absoluteString: f };
}
function z({
  locale: t,
  isUtc: o = !1,
  setIsUtc: p,
  useShortTimezone: d = !0
}) {
  const x = $(() => {
    const i = d ? "shortUTC" : "utc", l = d ? "shortLocal" : "local", m = j[t] || j.en;
    return o ? `${m[i]}` : `${m[l]}`;
  }, [t, d, o]);
  return /* @__PURE__ */ n(
    a,
    {
      component: "span",
      sx: {
        color: "inherit",
        cursor: "pointer",
        border: "1px solid",
        borderColor: "divider",
        borderRadius: "20px",
        padding: "4px 8px",
        lineHeight: 1
      },
      onClick: () => p(!o),
      children: x
    }
  );
}
function et({
  value: t,
  locale: o = "en",
  withoutSuffix: p = !1,
  from: d = "",
  to: x = "",
  type: i = "relative",
  tz: l = void 0,
  relativeRange: m = void 0,
  enableTooltip: g = !0,
  useShortTimezone: D = !1,
  disableTimezone: C = !1,
  placement: T = "top-end",
  format: v = "lll",
  mode: L = "all",
  ...r
}) {
  const { innerContent: U, popContent: h, isUtc: c, setIsUtc: f, relativeString: e } = w({
    value: t,
    locale: o,
    withoutSuffix: p,
    from: d,
    to: x,
    type: i === "all" ? "utc" : i,
    tz: l,
    relativeRange: m,
    format: v
  }), u = /* @__PURE__ */ I(
    a,
    {
      ...r,
      sx: [
        {
          display: "inline-flex",
          alignItems: "center",
          gap: 0.5
        },
        ...Array.isArray(r.sx) ? r.sx : [r.sx]
      ],
      children: [
        /* @__PURE__ */ n(a, { component: "span", ...r, sx: {}, children: U }),
        /* @__PURE__ */ n(a, { component: "span", sx: { color: "inherit" }, children: "·" }),
        /* @__PURE__ */ n(a, { component: "span", sx: { color: "inherit" }, children: e }),
        !C && /* @__PURE__ */ I(F, { children: [
          /* @__PURE__ */ n(a, { component: "span", sx: { color: "inherit" }, children: "·" }),
          /* @__PURE__ */ n(z, { locale: o, isUtc: c, setIsUtc: f, useShortTimezone: D })
        ] })
      ]
    }
  );
  return L === "daysLeft" ? /* @__PURE__ */ n(M, { title: u, placement: T, enterTouchDelay: 0, children: /* @__PURE__ */ n(a, { component: "span", children: e }) }) : i === "all" && L === "all" ? /* @__PURE__ */ n(M, { title: void 0, placement: T, enterTouchDelay: 0, children: u }) : /* @__PURE__ */ n(M, { title: g ? h : void 0, placement: T, enterTouchDelay: 0, children: /* @__PURE__ */ I(
    a,
    {
      sx: {
        display: "inline-flex",
        alignItems: "center",
        gap: 1
      },
      children: [
        /* @__PURE__ */ n(a, { component: "span", ...r, children: U }),
        i === "utc" && !C && /* @__PURE__ */ n(z, { locale: o, isUtc: c, setIsUtc: f, useShortTimezone: D })
      ]
    }
  ) });
}
export {
  et as default
};
