import { defineComponent as ce, resolveComponent as G, createBlock as de, openBlock as O, unref as l, isRef as Ee, withCtx as z, createElementVNode as te, createVNode as K, toDisplayString as ne, createTextVNode as ke, onMounted as ue, watch as b, ref as B, computed as oe, onBeforeMount as Ae, onBeforeUnmount as Ne, createElementBlock as V, Fragment as Te } from "vue";
import { detectLocale as F, useCoreStore as P } from "@feedmepos/mf-common";
import { u as $e, a as Be, b as re, c as Fe, d as qe, e as xe, f as Je, s as Qe } from "./vue-i18n-pKrrGi-M.js";
import { storeToRefs as w } from "pinia";
import { u as ae, a as Ue, b as je } from "./BaseDialog.vue_vue_type_script_setup_true_lang-C0TNaWyV.js";
import { u as ze, _ as Ve } from "./NavigationTab.vue_vue_type_script_setup_true_lang-DcX8TQAm.js";
import { u as se } from "./message-dialog-UoMWOnil.js";
import { d } from "./app-Bp5urT4H.js";
import { useRoute as Pe, useRouter as Ge } from "vue-router";
const Ke = { class: "fm-typo-en-body-md-400 text-ellipsis overflow-hidden" }, We = /* @__PURE__ */ ce({
  __name: "Dialog",
  setup(C) {
    const { t: c } = $e(), p = se(), { isOpened: g, message: I, title: h } = w(se());
    return (R, S) => {
      const E = G("FmButton"), q = G("FmDialog");
      return O(), de(q, {
        modelValue: l(g),
        "onUpdate:modelValue": S[0] || (S[0] = (x) => Ee(g) ? g.value = x : null),
        overlay: "",
        "close-button": !1
      }, {
        "dialog-header": z(() => [
          ke(ne(l(h)), 1)
        ]),
        default: z(() => [
          te("p", Ke, ne(l(I)), 1)
        ]),
        "dialog-footer": z(() => [
          S[1] || (S[1] = te("div", { class: "grow" }, null, -1)),
          K(E, {
            label: l(c)("report.common.cancel"),
            variant: "tertiary",
            onClick: l(p).dismiss
          }, null, 8, ["label", "onClick"]),
          K(E, {
            label: l(c)("report.common.confirm"),
            onClick: l(p).confirm
          }, null, 8, ["label", "onClick"])
        ]),
        _: 1
      }, 8, ["modelValue"]);
    };
  }
}), ie = B(F());
function He() {
  const C = (c) => {
    switch (ie.value = c, c) {
      case "zh-CN":
        d.locale("zh-cn");
        break;
      default:
        d.locale("en");
        break;
    }
  };
  return ue(() => {
    C(F());
  }), b(() => F(), C, { deep: !0, immediate: !0 }), {
    currentLocale: ie
  };
}
d.locale(F() === "zh-CN" ? "zh-cn" : "en");
const Xe = ["src"], Ye = ["src"], ut = /* @__PURE__ */ ce({
  __name: "App",
  setup(C) {
    const { businessId: c, token: p, country: g } = w(Be()), { sessionUser: I, currentBusiness: h, currentCountry: R, registerSidebarComponent: S } = P(), { getRestaurantAndWarehouse: E, getBusinessSetting: q, beginBusinessLoad: x, completeBusinessLoad: le } = re(), { getFeatureFlags: fe } = Fe(), { version: y } = w(ae()), { getSettings: me } = qe(), { promptLoader: pe } = xe(), s = Pe(), J = Ge(), L = Ue(), W = ze(), a = re(), ge = ae(), { suppressReportRouteSync: k } = w(ge), { getReports: ye, getOverviewReport: ve, getReportConfig: H, currentReportId: X } = L, { currentReportId: Se, reports: De } = w(L), { getSchema: Ie } = je(), { fetchSchedules: he } = W, { init: Re } = Je(), Q = B("v5"), A = B(!1), N = B(!0), Le = oe(() => `https://portal-v1.feedme.cc/b/${c.value}/report/CLOSE_UP?showHeader=false&country=${g.value.toUpperCase()}&menuOption=business-report&token=${p.value}&v2=true&reportV5Button=true`), _e = oe(() => `https://report-v4.feedme.cc?businessId=${c.value}&country=${g.value}&token=${p.value}&reportV3Button=true`);
    b(
      [() => h.value, () => I.value, () => R.value],
      () => {
        var e, n;
        const t = ((e = h.value) == null ? void 0 : e.id) ?? "";
        h.value && I.value && R.value && (Qe(R.value), c.value !== t && (c.value = t), p.value = ((n = I.value) == null ? void 0 : n.token) ?? "", g.value = R.value ?? "");
      },
      { immediate: !0 }
    );
    function Y(t) {
      if (typeof t.data == "string")
        try {
          const e = JSON.parse(t.data);
          (e == null ? void 0 : e.token) !== "" && (e == null ? void 0 : e.token) !== void 0 && (p.value = e.token);
        } catch (e) {
          console.error(e);
        }
    }
    function Z(t) {
      if (typeof t.data == "string")
        try {
          const e = JSON.parse(t.data);
          (e == null ? void 0 : e.v5) === !0 ? y.value = Q.value : (e == null ? void 0 : e.v3) === !0 && (y.value = "v3");
        } catch (e) {
          console.error(e);
        }
    }
    b(y, () => {
      ["v3", "v4"].includes(y.value) ? P().hideSidebarToggle(!0) : P().hideSidebarToggle(!1);
    }), Ae(() => {
      S(Ve);
    }), ue(() => {
      console.log("5.22.16", !0), window.addEventListener("message", Y), window.addEventListener("message", Z);
    }), Ne(() => {
      window.removeEventListener("message", Y), window.removeEventListener("message", Z), L.$dispose(), W.$dispose();
    });
    function Oe(t) {
      try {
        return decodeURIComponent(t);
      } catch {
        return t;
      }
    }
    function T(t) {
      if (typeof t != "string" || t.length === 0) return;
      const e = t.includes("%") ? [t, Oe(t)] : [t];
      for (const n of e)
        try {
          const o = JSON.parse(n);
          if (typeof o == "string")
            try {
              return JSON.parse(o);
            } catch {
              return o;
            }
          return o;
        } catch {
        }
    }
    function M(t) {
      if (!t || typeof t != "object") return !1;
      const e = t;
      return typeof e.hour == "number" && typeof e.minute == "number";
    }
    function be(t = s.query) {
      const e = t, n = e.startDate ? d(e.startDate).startOf("day").toISOString() : void 0, o = e.endDate ? d(e.endDate).endOf("day").toISOString() : void 0, f = typeof e.timeStart == "string", i = typeof e.timeEnd == "string", m = T(e.timeStart), v = T(e.timeEnd), u = M(m) ? m : null, $ = M(v) ? v : null, U = e.compareStartDate ? d(e.compareStartDate).startOf("day").toISOString() : void 0, j = e.compareEndDate ? d(e.compareEndDate).endOf("day").toISOString() : void 0, ee = e.isAllLocation === "true" || e.isAllLocation === void 0;
      let _ = [];
      if (ee)
        _ = a.selectedLocationIds ?? [];
      else {
        const r = T(e.locationIds);
        _ = Array.isArray(r) ? r : [];
      }
      let D = e.reportId || X;
      if (De.value.find((r) => r.id === D) || (D = X), D && D !== L.currentReportId && L.updateCurrentReportId(D), n || o) {
        const r = a.dateRange;
        (r.startDate !== n || r.endDate !== o) && a.setDateRange({ startDate: n, endDate: o });
      }
      if (f || i) {
        const r = a.timeRange;
        (r.start !== u || r.end !== $) && a.setTimeRange({ start: u, end: $ });
      }
      if (U || j) {
        const r = a.compareDateRange;
        (r.startDate !== U || r.endDate !== j) && a.setCompareDateRange({
          startDate: U,
          endDate: j
        });
      }
      if (!ee) {
        const r = a.selectedLocationIds;
        r.length === _.length && r.every((we, Ce) => we === _[Ce]) || a.setSelectedLocationIds(_);
      }
      return {
        // TODO: this returned value can used for getBusinessSetting
        reportId: D,
        startDate: e.startDate,
        endDate: e.endDate,
        timeStart: e.timeStart,
        timeEnd: e.timeEnd,
        compareStartDate: e.compareStartDate,
        compareEndDate: e.compareEndDate,
        locationIds: typeof e.locationIds == "string" ? e.locationIds : void 0,
        isAllLocation: e.isAllLocation
      };
    }
    return b(
      () => c.value,
      async (t, e) => {
        if (t !== "") {
          A.value = !1, x(t);
          const n = "mf-report-prev-business-id", o = (() => {
            try {
              return sessionStorage.getItem(n);
            } catch {
              return null;
            }
          })(), f = !!o && o !== t;
          let i = { ...s.query }, m = !1;
          if (!N.value || e !== void 0 || f) {
            a.setSelectedLocationIds([]);
            const { locationIds: v, ...u } = s.query;
            i = {
              ...u,
              isAllLocation: "true"
            }, m = !0, await J.replace({
              name: s.name,
              params: s.params,
              query: i
            });
          }
          k.value !== void 0 && (k.value = !0), await pe(async () => {
            await Re({
              apiKey: "6GhcttsJ9Rj9cTevcdrpin",
              url: "https://flag.feedme.farm/api/v1/",
              enableAnalytics: !1,
              cacheFlags: !1
            });
            const v = await H();
            if (Q.value = v.version, y.value = Q.value, await Promise.all([
              fe(),
              H(),
              ve(),
              ye(),
              Ie(),
              he(),
              E(
                (() => {
                  const u = T(i.locationIds);
                  return Array.isArray(u) ? u : void 0;
                })()
              ),
              me()
            ]), await q(), be(i), m) {
              const { locationIds: u, ...$ } = s.query;
              (s.query.locationIds !== void 0 || s.query.isAllLocation !== "true") && await J.replace({
                name: s.name,
                params: s.params,
                query: {
                  ...$,
                  isAllLocation: "true"
                }
              });
            }
            le(t);
            try {
              sessionStorage.setItem(n, t);
            } catch {
            }
          }), A.value = !0, N.value && (N.value = !1);
        }
      },
      {
        immediate: !0
      }
    ), b(
      () => (A.value, {
        reportId: Se.value,
        selectedLocationIds: a.selectedLocationIds,
        dateRange: a.dateRange,
        timeRange: a.timeRange,
        compareDateRange: a.compareDateRange
      }),
      (t) => {
        if (!A.value || !t.reportId || N.value) return;
        if (k.value) {
          k.value = !1;
          return;
        }
        const e = t.selectedLocationIds, n = t.dateRange, o = t.timeRange, f = t.compareDateRange, i = {
          ...s.query,
          reportId: t.reportId,
          startDate: n.startDate ? d(n.startDate).startOf("day").toISOString() : void 0,
          endDate: n.endDate ? d(n.endDate).endOf("day").toISOString() : void 0,
          compareStartDate: f.startDate ? d(f.startDate).startOf("day").toISOString() : void 0,
          compareEndDate: f.endDate ? d(f.endDate).endOf("day").toISOString() : void 0,
          timeStart: o.start ? JSON.stringify(o.start) : void 0,
          timeEnd: o.end ? JSON.stringify(o.end) : void 0
        }, m = e.length === a.locations.length;
        i.isAllLocation = String(m), m ? delete i.locationIds : e.length > 0 ? i.locationIds = JSON.stringify(e) : delete i.locationIds;
        const v = JSON.stringify(s.query), u = JSON.stringify(i);
        v !== u && J.replace({
          name: s.name,
          params: s.params,
          query: i
        });
      },
      { deep: !0 }
    ), He(), (t, e) => {
      const n = G("router-view");
      return O(), V(Te, null, [
        K(We),
        l(y) === "v3" ? (O(), V("iframe", {
          key: 0,
          class: "bg-white h-full w-full",
          src: Le.value
        }, null, 8, Xe)) : l(y) === "v4" ? (O(), V("iframe", {
          key: 1,
          class: "bg-white h-full w-full",
          src: _e.value
        }, null, 8, Ye)) : (O(), de(n, { key: 2 }))
      ], 64);
    };
  }
});
export {
  ut as default
};
