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