import A from "./TimeFrame.js";
import { getWeekendDays as k } from "../common/utils.js";
import { getEdgesDistance as I, withTimezone as S, getTimezoneDateParts as c, computeTimestampOffset as _, getMonthDays as d, startOfMonth as M, startOfWeek as R } from "../../utils.js";
import { DAY_OF_WEEK_FORMATS as H, MAXIMUM_MONTH_UNITS as E, DAY_MS as O } from "../../constants.js";
import L from "../common/flags.js";
import { TimeFlag as s } from "../../types.js";
import { timezoneToSystem as F, systemToTimezone as N } from "../../../../../../core/Localization/datetime/restamper/utils.js";
import { isUndefined as w, isString as C } from "../../../../../../utils/value/is.js";
import { isInfinity as v, isBitSafeInteger as x } from "../../../../../../utils/value/number.js";
import { enumerable as y } from "../../../../../../utils/struct/property.js";
import { struct as g, withFreezeProxyHandlers as U, structFrom as $ } from "../../../../../../utils/struct/main.js";
class tt extends A {
  #e = 7;
  #i = [];
  #s = k(this.firstWeekDay);
  #c;
  #r = -1 / 0;
  #o = 1 / 0;
  #l = 1 / 0;
  #n;
  #g;
  #m;
  #a;
  #h;
  daysInWeek = this.#e;
  constructor() {
    super(), this.initialize();
  }
  get fromTimestamp() {
    return this.#r;
  }
  get toTimestamp() {
    return this.#o;
  }
  get numberOfBlocks() {
    return this.#l;
  }
  get currentDayTimestamp() {
    return this.#c;
  }
  get dynamicBlockHeight() {
    return super.dynamicBlockHeight;
  }
  set dynamicBlockHeight(t) {
    const e = this.dynamicBlockHeight;
    super.dynamicBlockHeight = t, this.dynamicBlockHeight !== e && this.refreshFrame(!0);
  }
  get rowspan() {
    return this.#e;
  }
  get firstWeekDay() {
    return super.firstWeekDay;
  }
  set firstWeekDay(t) {
    const e = this.firstWeekDay;
    super.firstWeekDay = t, this.firstWeekDay !== e && (this.#i.length = 0, this.#s = k(this.firstWeekDay), this.reoriginate(), this.refreshFrame());
  }
  #u(t) {
    const e = I(t, this.originTimestamp, this.timezone);
    return t < this.originTimestamp ? 0 - e : e;
  }
  #p(t, e = 0) {
    const i = S(this.timezone), m = F(i, t), n = N(i, m + e * O);
    let [, , , r, a] = c(n, this.timezone), l = 0;
    return (r > 0 || a > 0) && (r = (r > 12 ? 24 : 0) - r, a = (r > 1 ? 1 : -1) * a, l = r * 36e5 + a * 6e4), n + l;
  }
  #t(t) {
    return w(t) || v(t) ? t : t - _(t, this.timezone);
  }
  #f() {
    this.#a = this.#t(this.selectionStart), this.#h = this.#t(this.selectionEnd);
  }
  getCursorBlockOriginTimestampOffset(t) {
    return c(t, this.timezone)[2] - 1;
  }
  getDayOfWeekAtIndex(t) {
    if (!this.#i[t]) {
      const e = new Date(this.getTimestampAtIndex(t));
      let i = 0;
      this.#s.includes(t) && (i |= s.WEEKEND), t === 0 ? i |= s.LINE_START : t === 6 && (i |= s.LINE_END);
      const m = {};
      for (const n of H)
        m[n] = y(
          e.toLocaleDateString(this.locale, { weekday: n, timeZone: this.timezone })
        );
      this.#i[t] = g({
        flags: y(L(i)),
        labels: y(g(m))
      });
    }
    return this.#i[t];
  }
  getEdgeBlockOffsetsFromOrigin() {
    return [this.#u(this.#r), this.#u(this.#o)];
  }
  getFormattedDataForBlockCell(t) {
    const [e, i, m] = c(t, this.timezone);
    return [Number(m).toLocaleString(this.locale), `${e}-${`${i + 1}`.padStart(2, "0")}-${`${m}`.padStart(2, "0")}`];
  }
  getFormattedDataForFrameBlock(t) {
    const [e, i] = c(t, this.timezone);
    return [
      new Date(t).toLocaleDateString(this.locale, { month: "long", year: "numeric", timeZone: this.timezone }),
      `${e}-${`${i + 1}`.padStart(2, "0")}`
    ];
  }
  getFrameBlockAtIndex(t) {
    const [e, i, m] = d(this.origin, this.#m, t), n = t > 0 ? this.getFrameBlockAtIndex(t - 1).inner.to + 1 : this.#n, r = n + e - 1, a = Math.floor(n / 7) * 7, l = this.dynamicBlockHeight ? Math.ceil((r + 1) / 7) * 7 : a + E, D = this.dynamicBlockHeight ? l - a : E, B = new Proxy(
      g(),
      U({
        get: (W, p, z) => {
          if (C(p)) {
            const u = +p;
            if (x(u) && u >= 0 && u < D) {
              const f = a + u, h = this.getTimestampAtIndex(f), T = f % this.#e;
              let o = h === this.currentDayTimestamp ? s.CURRENT : 0;
              return f === this.cursor && (o |= s.CURSOR), this.#s.includes(T) && (o |= s.WEEKEND), T === 0 ? o |= s.LINE_START : T === this.#e - 1 && (o |= s.LINE_END), f >= n && f <= r && (f === n ? o |= s.BLOCK_START : f === r && (o |= s.BLOCK_END), o |= s.WITHIN_BLOCK), h >= this.fromTimestamp && h <= this.toTimestamp && (h === this.fromTimestamp && (o |= s.RANGE_START), h === this.toTimestamp && (o |= s.RANGE_END), o |= s.WITHIN_RANGE), h >= this.#a && h <= this.#h && (h === this.#a && (o |= s.SELECTION_START), h === this.#h && (o |= s.SELECTION_END), o |= s.WITHIN_SELECTION), [h, o];
            }
          }
          return Reflect.get(W, p, z);
        }
      })
    );
    return $(B, {
      inner: {
        value: g({
          from: { value: n },
          to: { value: r },
          units: { value: e }
        })
      },
      month: { value: i },
      outer: {
        value: g({
          from: { value: a },
          to: { value: l - 1 },
          units: { value: D }
        })
      },
      year: { value: m }
    });
  }
  getUnitsForFrameBlockAtIndex(t) {
    return d(this.origin, this.#m, t)[0];
  }
  getUnitsOffsetForTimestamp(t, e) {
    return Math.round((e - t) / O);
  }
  reoriginate() {
    this.originTimestamp = M(this.originTimestamp, this.timezone);
    const [t, e] = c(this.originTimestamp, this.timezone), i = R(this.originTimestamp, this.timezone, this.firstWeekDay);
    this.origin = e, this.#m = t, this.#n = this.getUnitsOffsetForTimestamp(i, this.originTimestamp), this.#g = this.#p(this.originTimestamp, -this.#n);
  }
  reslice() {
    this.#f(), this.#r = this.#t(super.fromTimestamp), this.#o = this.#t(super.toTimestamp), this.#l = I(super.fromTimestamp, super.toTimestamp, this.timezone) + 1;
  }
  shiftOrigin(t) {
    const [e, i] = c(this.originTimestamp, this.timezone), [, m, n] = d(i, e, t), r = S(this.timezone), a = new Date(F(r, this.originTimestamp)).setFullYear(n, m);
    this.originTimestamp = N(r, a), this.reoriginate();
  }
  clearSelection() {
    super.clearSelection(), this.#f(), this.refreshFrame(!0);
  }
  getTimestampAtIndex(t) {
    return this.#p(this.#g, t);
  }
  updateSelection(t, e) {
    super.updateSelection(t, e), this.#f(), this.refreshFrame(!0);
  }
  withCurrentDayTimestamp() {
    this.#c = this.#t(Date.now());
  }
}
export {
  tt as default
};
