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