import { defineComponent as Ce, mergeModels as Se, useModel as De, ref as z, computed as g, resolveComponent as x, createBlock as S, openBlock as u, withCtx as _, createElementVNode as h, createVNode as i, unref as o, normalizeClass as Ue, createTextVNode as ze, toDisplayString as E, createElementBlock as R, Fragment as xe, renderSlot as ct, createCommentVNode as U, watch as ge, renderList as Be, onMounted as pt, onUnmounted as Vt, resolveDynamicComponent as Tt, isRef as vt, normalizeStyle as et } from "vue";
import { W as $t, g as St, _ as Dt, a as Ot, e as Ie, d as ft, b as gt, S as Ae, T as Nt, c as It } from "./TransitionFade-BYS_PD1Y.js";
import { C as Te, D as oe, c as Re, g as Rt, u as lt, M as bt, r as Mt, d as rt, n as Bt, e as Ut, f as qt, h as Et, i as Pt, a as zt, j as it, k as Qe, m as Lt, Q as We, o as Gt, l as At, b as st, p as Wt } from "./GlobalFilterSelect.vue_vue_type_script_setup_true_lang-DXtWF7LP.js";
import { storeToRefs as Me } from "pinia";
import { useRouter as ot, useRoute as at } from "vue-router";
import { useBreakpoints as He, useSnackbar as yt, FmButtonColorThemeVariant as $e } from "@feedmepos/ui-library";
import { u as nt } from "./message-dialog-UoMWOnil.js";
import { u as qe, a as Ht } from "./_plugin-vue_export-helper-onplcgja.js";
import { r as Le } from "./i18n-CI_sQ5d_.js";
import { _ as Ye } from "./TileGrid.vue_vue_type_script_setup_true_lang-jbujDyFY.js";
import { a as Xe, _ as Ze } from "./SelectComponent.vue_vue_type_script_setup_true_lang-CGbb3b5G.js";
import { i as jt } from "./DynamicWidget.vue_vue_type_script_setup_true_lang-BGD1_4cA.js";
import { useCoreStore as Jt } from "@feedmepos/mf-common";
import { u as Kt, a as Qt } from "./app-dOrzXjgr.js";
import { d as Yt } from "./DateRangeSelect.vue_vue_type_script_setup_true_lang-BuO42-MR.js";
const Xt = /* @__PURE__ */ Ce({
  __name: "WidgetDialog",
  props: /* @__PURE__ */ Se({
    isOpen: { type: Boolean }
  }, {
    modelValue: {},
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ Se(["update:isOpen"], ["update:modelValue"]),
  setup(M, { emit: d }) {
    const { t: a } = qe(), { breakpoints: c } = He(), v = De(M, "modelValue"), b = M, B = d;
    function Y() {
      B("update:isOpen", !1);
    }
    function k() {
      y.value && (v.value = y.value, y.value = void 0, B("update:isOpen", !1));
    }
    const y = z(), C = g(() => !y.value);
    return (O, G) => {
      const D = x("FmButton"), V = x("FmDialog");
      return u(), S(V, {
        "model-value": b.isOpen
      }, {
        "dialog-header": _(() => [
          ze(E(o(a)("report.widget.type")), 1)
        ]),
        default: _(() => [
          i($t, {
            modelValue: y.value,
            "onUpdate:modelValue": G[0] || (G[0] = (T) => y.value = T),
            class: Ue(`grid ${o(c).lg || o(c).md ? "grid-cols-3 gap-3" : "grid-cols-2 gap-3"}`),
            "child-class": "p-[8px] flex flex-col"
          }, null, 8, ["modelValue", "class"])
        ]),
        "dialog-footer": _(() => [
          G[1] || (G[1] = h("div", { class: "grow" }, null, -1)),
          i(D, {
            label: o(a)("report.common.cancel"),
            variant: "tertiary",
            onClick: Y
          }, null, 8, ["label"]),
          i(D, {
            label: o(a)("report.common.confirm"),
            disabled: C.value,
            onClick: k
          }, null, 8, ["label", "disabled"])
        ]),
        _: 1
      }, 8, ["model-value"]);
    };
  }
}), Zt = { class: "fm-typo-en-body-lg-600" }, tt = /* @__PURE__ */ Ce({
  __name: "ReportEditorTabHeader",
  props: /* @__PURE__ */ Se({
    icon: {
      type: String,
      required: !0
    }
  }, {
    modelValue: { type: Boolean, required: !0, default: !1 },
    modelModifiers: {}
  }),
  emits: ["update:modelValue"],
  setup(M) {
    const { t: d } = qe(), a = De(M, "modelValue"), c = M;
    return (v, b) => {
      const B = x("FmIcon");
      return u(), R("div", {
        class: "px-6 py-16 flex items-center gap-2 bg-fm-color-neutral-white border cursor-pointer",
        onClick: b[0] || (b[0] = (Y) => a.value = !a.value)
      }, [
        h("p", Zt, E(o(d)("report.editor.customizeReport")), 1),
        b[1] || (b[1] = h("div", { class: "grow" }, null, -1)),
        i(B, {
          name: c.icon
        }, null, 8, ["name"])
      ]);
    };
  }
}), el = /* @__PURE__ */ Ce({
  __name: "ReportEditorTabDesktop",
  props: {
    modelValue: { required: !0, default: !1 },
    modelModifiers: {}
  },
  emits: ["update:modelValue"],
  setup(M) {
    const d = De(M, "modelValue");
    return (a, c) => (u(), R(xe, null, [
      i(tt, {
        modelValue: d.value,
        "onUpdate:modelValue": c[0] || (c[0] = (v) => d.value = v),
        icon: "keyboard_double_arrow_left"
      }, null, 8, ["modelValue"]),
      ct(a.$slots, "default")
    ], 64));
  }
}), tl = /* @__PURE__ */ Ce({
  __name: "ReportEditorTabMobile",
  props: {
    modelValue: { required: !0, default: !1 },
    modelModifiers: {}
  },
  emits: ["update:modelValue"],
  setup(M) {
    const d = De(M, "modelValue");
    return (a, c) => {
      const v = x("FmTopSheet"), b = x("FmCard");
      return u(), S(b, { class: "fixed bottom-0 w-full" }, {
        default: _(() => [
          i(tt, {
            modelValue: d.value,
            "onUpdate:modelValue": c[0] || (c[0] = (B) => d.value = B),
            icon: "keyboard_double_arrow_up"
          }, null, 8, ["modelValue"]),
          i(v, {
            modelValue: d.value,
            "onUpdate:modelValue": c[2] || (c[2] = (B) => d.value = B),
            "fullscreen-size": "sm"
          }, {
            default: _(() => [
              i(tt, {
                modelValue: d.value,
                "onUpdate:modelValue": c[1] || (c[1] = (B) => d.value = B),
                icon: "keyboard_double_arrow_down"
              }, null, 8, ["modelValue"]),
              d.value ? ct(a.$slots, "default", { key: 0 }) : U("", !0)
            ]),
            _: 3
          }, 8, ["modelValue"])
        ]),
        _: 3
      });
    };
  }
}), ll = {
  key: 2,
  class: "flex flex-col gap-3 mt-2 border border-neutral-300 p-3"
}, ol = { class: "grid grid-cols-1 gap-2 rounded-md border border-neutral-300 p-2 sm:grid-cols-[1.3fr_1fr_1fr_auto] sm:items-end" }, al = {
  key: 0,
  class: "flex justify-end sm:pb-[2px]"
}, nl = { class: "fm-typo-en-body-lg-600 text-fm-color-typo-primary" }, rl = { class: "pl-1" }, il = {
  key: 0,
  class: "flex justify-end sm:pb-[2px]"
}, sl = {
  key: 3,
  class: "flex flex-col gap-2"
}, ul = /* @__PURE__ */ Ce({
  __name: "GlobalFilterDialog",
  props: /* @__PURE__ */ Se({
    schemas: {
      type: Array,
      default: () => []
    },
    tiles: {
      type: Array,
      required: !0
    }
  }, {
    globalFilter: {
      default: () => null
    },
    globalFilterModifiers: {},
    isOpen: { type: Boolean, default: () => !1, required: !0 },
    isOpenModifiers: {}
  }),
  emits: ["update:globalFilter", "update:isOpen"],
  setup(M) {
    const { t: d, te: a } = qe(), c = De(M, "globalFilter"), v = De(M, "isOpen"), b = yt(), B = M, Y = z(""), k = z(), y = z(), C = z(), O = z([]), G = z([]), D = z(!1), V = z([]), T = z([]), X = z(Te.Equal);
    function ae(l) {
      return JSON.parse(JSON.stringify(l));
    }
    function Fe(l, e) {
      return { hour: l, minute: e };
    }
    function ue(l) {
      return l ? `${String(l.hour).padStart(2, "0")}:${String(l.minute).padStart(2, "0")}` : null;
    }
    function j(l) {
      if (!l) return null;
      const [e, s] = l.split(":").map(Number);
      return Number.isNaN(e) || Number.isNaN(s) ? null : { hour: e, minute: s };
    }
    const w = g(() => y.value ? !(y.value.type === oe.Timestamp && C.value === Re.DateTime) : !1), Z = g(
      () => {
        var l;
        return ((l = y.value) == null ? void 0 : l.type) === oe.Timestamp && C.value === Re.TimeOfDay;
      }
    ), ne = g(() => {
      var l;
      return (((l = y.value) == null ? void 0 : l.filterOperators) ?? []).filter((e) => e !== Te.And && e !== Te.Or).map((e) => ({
        label: d(`report.section.comparisonOperator.${e}`),
        value: e
      }));
    });
    function te() {
      var e;
      const l = (e = ne.value[0]) == null ? void 0 : e.value;
      return l || (y.value ? H(
        y.value.type,
        C.value ?? null
      ) : Te.Equal);
    }
    function re(l) {
      if (y.value)
        return Rt({
          operator: l ?? te(),
          dataType: y.value.type,
          dimensionOption: y.value.type === oe.Timestamp ? C.value ?? null : null,
          t: d
        });
    }
    function ce(l, e) {
      if (typeof (e == null ? void 0 : e.default) == "boolean" || l.length > 0 && l.every((F) => typeof F == "boolean")) {
        const F = l.reduce((I, J) => (typeof J == "boolean" && I.push(J), I), []);
        if (e)
          for (e.fixed && F.splice(e.count); F.length < e.count; )
            F.push(typeof e.default == "boolean" ? e.default : !1);
        return F;
      }
      const p = l.reduce((F, I) => (typeof I == "string" && F.push(I), F), []);
      if (e)
        for (e.fixed && p.splice(e.count); p.length < e.count; )
          p.push(typeof e.default == "string" ? e.default : "");
      return p;
    }
    function pe(l = X.value) {
      const e = re(l), s = ce(
        e ? Array(e.count).fill(e.default) : [],
        e
      );
      return {
        label: "",
        operator: l,
        values: s
      };
    }
    function q() {
      return {
        name: "",
        startTime: Fe(0, 0),
        endTime: Fe(23, 59)
      };
    }
    function L() {
      if (!(!w.value || !D.value)) {
        if (Z.value) {
          T.value.length === 0 && (T.value = [q()]);
          return;
        }
        V.value.length === 0 && (V.value = [pe()]);
      }
    }
    function K() {
      var s, p, F;
      const l = ae(((s = c.value) == null ? void 0 : s.customOptions) ?? []), e = ae(((p = c.value) == null ? void 0 : p.customTimeRanges) ?? []);
      X.value = ((F = l[0]) == null ? void 0 : F.operator) ?? te(), D.value = l.length > 0 || e.length > 0, V.value = l, T.value = e, L();
    }
    function ve(l) {
      X.value = l, V.value = V.value.map((e, s) => {
        const p = V.value[s];
        if (!p)
          return pe(l);
        const F = re(l), I = ce(
          Array.isArray(p.values) ? [...p.values] : [],
          F
        );
        return {
          ...p,
          operator: l,
          values: I
        };
      });
    }
    function be() {
      V.value = [...V.value, pe()];
    }
    function Q(l) {
      V.value.length <= 1 || (V.value = V.value.filter((e, s) => s !== l));
    }
    function ke() {
      T.value = [...T.value, q()];
    }
    function ie(l) {
      T.value.length <= 1 || (T.value = T.value.filter(
        (e, s) => s !== l
      ));
    }
    function fe(l, e, s) {
      const p = T.value[l];
      p && (T.value[l] = {
        ...p,
        [e]: j(s)
      });
    }
    function Ve(l) {
      return l.length === 0 || l.some((e) => e === "");
    }
    function Oe() {
      if (!D.value) return !0;
      if (Z.value) {
        const s = T.value.map((F) => F.name.trim()).filter(Boolean);
        return T.value.some(
          (F) => !F.name.trim() || !F.startTime || !F.endTime
        ) ? (b.open({
          message: d("report.section.filter.invalidCustomTimeRange"),
          type: "error"
        }), !1) : new Set(s).size !== s.length ? (b.open({
          message: d("report.section.filter.customTimeRangeNamesMustBeUnique"),
          type: "error"
        }), !1) : !0;
      }
      const l = V.value.map((s) => s.label.trim()).filter(Boolean);
      return V.value.some(
        (s) => !s.label.trim() || Ve(s.values)
      ) ? (b.open({ message: d("report.section.filter.invalidCustomOption"), type: "error" }), !1) : new Set(l).size !== l.length ? (b.open({
        message: d("report.section.filter.customOptionLabelsMustBeUnique"),
        type: "error"
      }), !1) : !0;
    }
    ge(v, (l) => {
      var e, s, p, F, I;
      l && (Y.value = ((e = c.value) == null ? void 0 : e.label) || "", k.value = B.schemas.find(
        (J) => {
          var le;
          return J.name === ((le = c.value) == null ? void 0 : le.schemaName);
        }
      ), y.value = (s = he.value.find(
        (J) => {
          var le;
          return J.value.name === ((le = c.value) == null ? void 0 : le.name);
        }
      )) == null ? void 0 : s.value, C.value = (p = c.value) == null ? void 0 : p.option, O.value = ((F = c.value) == null ? void 0 : F.tileIds) || [], G.value = ((I = c.value) == null ? void 0 : I.filters) || [], K());
    }), ge(D, (l) => {
      var e;
      l && (X.value = ((e = V.value[0]) == null ? void 0 : e.operator) ?? te(), L());
    }), ge(w, (l) => {
      if (!l) {
        D.value = !1, V.value = [], T.value = [];
        return;
      }
      L();
    }), ge(Z, () => {
      L();
    }), ge(ne, (l) => {
      var s;
      const e = (s = l[0]) == null ? void 0 : s.value;
      e && (l.some((p) => p.value === X.value) || ve(e));
    });
    const ye = g(
      () => B.schemas.map((l) => ({
        label: l.label || l.name,
        value: l
      }))
    ), he = g(() => {
      var l;
      return (((l = k.value) == null ? void 0 : l.fields) || []).filter((e) => e.type !== oe.Numeric).map((e) => ({
        label: e.label || e.name,
        value: e
      }));
    }), Ee = g(() => {
      var l;
      return ((l = y.value) == null ? void 0 : l.type) !== oe.Timestamp ? [] : St(d);
    }), n = g(() => B.tiles.reduce((l, e, s) => {
      var I, J, le, me, _e;
      const p = ((I = e.widget) == null ? void 0 : I.config.schemaNames) ?? [], F = (((J = e.widget) == null ? void 0 : J.config.joinDatasources) ?? []).flatMap(
        (Ne) => Ne.schemaNames ?? []
      );
      return [...p, ...F].includes(((le = k.value) == null ? void 0 : le.name) || "") && l.push({
        label: (_e = (me = e.widget) == null ? void 0 : me.title) != null && _e.trim() ? e.widget.title : `Widget ${s + 1}`,
        value: e.id
      }), l;
    }, [])), m = g(
      () => B.schemas.flatMap(
        (l) => (l.fields ?? []).map((e) => ({
          ...e,
          labels: [
            Le(d, a, l.label, "schema"),
            Le(d, a, e.label || e.name, "other")
          ]
        }))
      )
    );
    function P(l) {
      var e;
      l.name !== ((e = k.value) == null ? void 0 : e.name) && (k.value = l, y.value = void 0, X.value = Te.Equal, D.value = !1, V.value = [], T.value = []);
    }
    function ee(l) {
      y.value = l, C.value = l.type === oe.Timestamp ? Re.DateTime : void 0, X.value = te(), D.value = !1, V.value = [], T.value = [];
    }
    function H(l, e) {
      switch (l) {
        case oe.Timestamp:
          return e === Re.DateTime || e === Re.TimeOfDay ? Te.Between : Te.Equal;
        case oe.Bool:
        case oe.String:
        case oe.Numeric:
          return Te.Equal;
        default:
          throw new Error(`Unsupported data type: ${l}`);
      }
    }
    function A() {
      if (!k.value) {
        b.open({ message: d("report.section.filter.invalidSchema"), type: "error" });
        return;
      }
      if (!y.value) {
        b.open({ message: d("report.section.filter.invalidDimension"), type: "error" });
        return;
      }
      const l = G.value.filter((p) => p ? !ye.value.some((I) => I.value.name === p.schemaName) : !0);
      if (l.length > 0) {
        const p = l.filter((I) => !!I).map((I) => I.schemaName), F = l.some((I) => !I) ? `, ${d("report.section.filter.unknownFilters")}` : "";
        b.open({
          message: d("report.section.filter.invalidFiltersSchemaNotFound", {
            schemas: p.join(", "),
            unknownFiltersSuffix: F
          }),
          type: "error"
        });
        return;
      }
      if (!Oe())
        return;
      const e = D.value && !Z.value ? V.value.map((p) => ({
        label: p.label.trim(),
        operator: p.operator ?? te(),
        values: ce(
          [...p.values],
          re(p.operator)
        )
      })) : void 0, s = D.value && Z.value ? T.value.map((p) => ({
        name: p.name.trim(),
        startTime: p.startTime,
        endTime: p.endTime
      })) : void 0;
      c.value = {
        schemaName: k.value.name,
        name: y.value.name,
        label: Y.value,
        operator: H(
          y.value.type,
          y.value.type === oe.Timestamp ? C.value ?? Re.DateTime : null
        ),
        option: y.value.type === oe.Timestamp ? C.value : null,
        values: [],
        tileIds: [...O.value],
        filters: ae(G.value),
        customOptions: e,
        customTimeRanges: s
      }, v.value = !1;
    }
    function W() {
      v.value = !1;
    }
    const se = g(() => {
      var l;
      return [
        (l = k.value) == null ? void 0 : l.name,
        ...G.value.map((e) => e.schemaName)
      ];
    });
    function de(l) {
      var t;
      const e = B.tiles.find((f) => f.id === l.value);
      if (!e || !k.value || !y.value) return !1;
      const s = (t = e == null ? void 0 : e.widget) == null ? void 0 : t.config, p = (s == null ? void 0 : s.schemaNames) ?? [], F = ((s == null ? void 0 : s.joinDatasources) ?? []).flatMap(
        (f) => f.schemaNames ?? []
      ), I = [...p, ...F];
      if (se.value.filter((f) => !!f).some((f) => !I.includes(f))) return !0;
      if (y.value.type === oe.Timestamp && C.value === Re.DateTime) return !1;
      const me = ((s == null ? void 0 : s.dimensions) ?? []).map((f) => ({
        schemaName: f.schemaName,
        name: f.name
      })), _e = ((s == null ? void 0 : s.joinDatasources) ?? []).flatMap(
        (f) => (f.dimensions ?? []).map(($) => ({
          schemaName: $.schemaName,
          name: $.name
        }))
      ), Ne = [...me, ..._e], Pe = k.value.name, r = y.value.name;
      return !Ne.some(
        (f) => f.schemaName === Pe && f.name === r
      );
    }
    return (l, e) => {
      const s = x("FmTextField"), p = x("FmSelect"), F = x("FmCheckbox"), I = x("FmTimePicker"), J = x("FmButton"), le = x("FmLabel"), me = x("FmIcon"), _e = x("FmTooltip"), Ne = x("FmForm"), Pe = x("FmDialog");
      return u(), S(Pe, {
        modelValue: v.value,
        "onUpdate:modelValue": e[9] || (e[9] = (r) => v.value = r),
        overlay: !0,
        "z-index": 10
      }, {
        "dialog-header": _(() => [
          ze(E(o(d)("report.section.filter.globalFilter")), 1)
        ]),
        default: _(() => [
          i(Ne, {
            ref: "form",
            class: "flex flex-col gap-2 w-[490px]"
          }, {
            default: _(() => {
              var r;
              return [
                i(s, {
                  modelValue: Y.value,
                  "onUpdate:modelValue": e[0] || (e[0] = (t) => Y.value = t),
                  placeholder: o(d)("report.section.filter.displayName"),
                  label: o(d)("report.section.filter.displayName"),
                  class: "mb-2"
                }, null, 8, ["modelValue", "placeholder", "label"]),
                i(Xe, {
                  "model-value": k.value,
                  items: ye.value,
                  placeholder: o(d)("report.section.schema.title"),
                  label: o(d)("report.section.schema.title"),
                  "onUpdate:modelValue": e[1] || (e[1] = (t) => P(t))
                }, null, 8, ["model-value", "items", "placeholder", "label"]),
                i(Xe, {
                  "model-value": y.value,
                  items: he.value,
                  placeholder: o(d)("report.section.dimension.title"),
                  label: o(d)("report.section.dimension.title"),
                  "onUpdate:modelValue": e[2] || (e[2] = (t) => ee(t))
                }, null, 8, ["model-value", "items", "placeholder", "label"]),
                ((r = y.value) == null ? void 0 : r.type) === o(oe).Timestamp ? (u(), S(p, {
                  key: 0,
                  modelValue: C.value,
                  "onUpdate:modelValue": e[3] || (e[3] = (t) => C.value = t),
                  items: Ee.value,
                  placeholder: o(d)("report.section.filter.timeFormatter"),
                  label: o(d)("report.section.filter.timeFormatter")
                }, null, 8, ["modelValue", "items", "placeholder", "label"])) : U("", !0),
                w.value ? (u(), S(F, {
                  key: 1,
                  modelValue: D.value,
                  "onUpdate:modelValue": e[4] || (e[4] = (t) => D.value = t),
                  label: o(d)("report.section.filter.customOptions"),
                  value: !0
                }, null, 8, ["modelValue", "label"])) : U("", !0),
                w.value && D.value ? (u(), R("div", ll, [
                  Z.value ? (u(!0), R(xe, { key: 0 }, Be(T.value, (t, f) => (u(), R("div", {
                    key: `custom-time-range-${f}`,
                    class: "flex flex-col gap-2"
                  }, [
                    h("div", ol, [
                      i(s, {
                        modelValue: t.name,
                        "onUpdate:modelValue": ($) => t.name = $,
                        label: o(d)("report.section.filter.label")
                      }, null, 8, ["modelValue", "onUpdate:modelValue", "label"]),
                      i(I, {
                        "model-value": ue(t.startTime),
                        label: o(d)("report.date.startingTime"),
                        "onUpdate:modelValue": ($) => fe(f, "startTime", $)
                      }, null, 8, ["model-value", "label", "onUpdate:modelValue"]),
                      i(I, {
                        "model-value": ue(t.endTime),
                        label: o(d)("report.date.endingTime"),
                        "onUpdate:modelValue": ($) => fe(f, "endTime", $)
                      }, null, 8, ["model-value", "label", "onUpdate:modelValue"]),
                      T.value.length > 1 ? (u(), R("div", al, [
                        i(J, {
                          icon: "delete",
                          variant: "tertiary",
                          onClick: ($) => ie(f)
                        }, null, 8, ["onClick"])
                      ])) : U("", !0)
                    ])
                  ]))), 128)) : (u(), R(xe, { key: 1 }, [
                    h("p", nl, E(o(d)("report.section.filter.operator")), 1),
                    ne.value.length > 0 ? (u(), S(Xe, {
                      key: 0,
                      "model-value": X.value,
                      items: ne.value,
                      placeholder: o(d)("report.section.filter.operator"),
                      "onUpdate:modelValue": e[5] || (e[5] = (t) => ve(t))
                    }, null, 8, ["model-value", "items", "placeholder"])) : U("", !0),
                    (u(!0), R(xe, null, Be(V.value, (t, f) => (u(), R("div", {
                      key: `custom-option-${f}`,
                      class: "flex flex-col gap-2"
                    }, [
                      h("div", {
                        class: Ue(["grid grid-cols-1 gap-2 p-2 sm:items-end text-fm-color-typo-primary fm-typo-en-body-lg-600 border-neutral-300 rounded-md border", "sm:grid-cols-[1fr_1.6fr_auto]"])
                      }, [
                        h("span", rl, E(o(d)("report.section.filter.label")), 1),
                        i(s, {
                          modelValue: t.label,
                          "onUpdate:modelValue": ($) => t.label = $
                        }, null, 8, ["modelValue", "onUpdate:modelValue"]),
                        (u(), S(Dt, {
                          key: `custom-option-value-${f}-${t.operator ?? ""}-${C.value ?? ""}`,
                          modelValue: t.values,
                          "onUpdate:modelValue": ($) => t.values = $,
                          config: re(t.operator),
                          option: C.value
                        }, null, 8, ["modelValue", "onUpdate:modelValue", "config", "option"])),
                        V.value.length > 1 ? (u(), R("div", il, [
                          i(J, {
                            icon: "delete",
                            variant: "tertiary",
                            onClick: ($) => Q(f)
                          }, null, 8, ["onClick"])
                        ])) : U("", !0)
                      ])
                    ]))), 128))
                  ], 64)),
                  i(J, {
                    class: "w-full",
                    variant: "primary",
                    icon: "add",
                    "icon-position": "prepend",
                    onClick: e[6] || (e[6] = (t) => Z.value ? ke() : be())
                  })
                ])) : U("", !0),
                i(Ot, {
                  modelValue: G.value,
                  "onUpdate:modelValue": e[7] || (e[7] = (t) => G.value = t),
                  options: m.value
                }, null, 8, ["modelValue", "options"]),
                i(le, { class: "mt-2 fm-typo-en-body-lg-600" }, {
                  default: _(() => [
                    ze(E(o(d)("report.section.filter.applyWidget")), 1)
                  ]),
                  _: 1
                }),
                M.tiles.length > 0 ? (u(), R("div", sl, [
                  (u(!0), R(xe, null, Be(n.value, (t) => (u(), R("div", {
                    key: t.value,
                    class: "flex items-center"
                  }, [
                    i(F, {
                      modelValue: O.value,
                      "onUpdate:modelValue": e[8] || (e[8] = (f) => O.value = f),
                      label: t.label,
                      value: t.value
                    }, null, 8, ["modelValue", "label", "value"]),
                    de(t) ? (u(), S(_e, {
                      key: 0,
                      content: o(d)("report.section.filter.missingFilterSchemaOrDimension"),
                      "z-index": 99,
                      class: "ml-2"
                    }, {
                      default: _(() => [
                        i(me, {
                          name: "warning",
                          class: "text-fm-color-system-error-300"
                        })
                      ]),
                      _: 1
                    }, 8, ["content"])) : U("", !0)
                  ]))), 128))
                ])) : (u(), S(le, {
                  key: 4,
                  class: "fm-typo-en-body-lg-400",
                  label: o(d)("report.section.filter.noWidget")
                }, null, 8, ["label"]))
              ];
            }),
            _: 1
          }, 512)
        ]),
        "dialog-footer": _(() => [
          e[10] || (e[10] = h("div", { class: "grow" }, null, -1)),
          i(J, {
            label: o(d)("report.common.cancel"),
            variant: "tertiary",
            onClick: W
          }, null, 8, ["label"]),
          i(J, {
            label: o(d)("report.common.confirm"),
            onClick: A
          }, null, 8, ["label"])
        ]),
        _: 1
      }, 8, ["modelValue"]);
    };
  }
}), dl = 8.4375, ml = 1024 ** 4, cl = {
  MY: { rate: 4, currency: "MYR", symbol: "RM" },
  TH: { rate: 33, currency: "THB", symbol: "฿" },
  SG: { rate: 1.29, currency: "SGD", symbol: "S$" },
  ID: { rate: 17900, currency: "IDR", symbol: "Rp" }
}, pl = { rate: 1, currency: "USD", symbol: "$" };
function ut(M, d) {
  const a = M / ml * dl, { rate: c, currency: v, symbol: b } = cl[d] ?? pl;
  return {
    amount: a * c,
    currency: v,
    symbol: b,
    costUsd: a
  };
}
function dt(M, d) {
  return M < 0.01 ? `< ${d}0.01` : `${d}${M.toFixed(2)}`;
}
const vl = { class: "flex flex-col gap-16" }, fl = {
  key: 0,
  class: "flex flex-col gap-8"
}, gl = { class: "flex items-center gap-4 pb-8 border-b border-neutral-300" }, bl = ["title"], yl = {
  key: 1,
  class: "rounded-md border border-dashed border-neutral-300 p-12 text-center fm-typo-en-body-sm-400 text-fm-color-typo-secondary"
}, hl = ["onClick"], _l = ["title"], wl = {
  key: 0,
  class: "text-fm-color-typo-secondary"
}, xl = { class: "text-fm-color-system-error-300" }, Cl = { class: "w-full text-left" }, Fl = /* @__PURE__ */ Ce({
  __name: "TileDrillPanel",
  props: {
    report: {}
  },
  emits: ["add-widget"],
  setup(M, { emit: d }) {
    const a = M, c = d, { t: v, te: b } = qe(), B = ot(), Y = at(), { promptMessage: k } = nt(), y = lt(), C = g(() => {
      const n = [];
      let m = a.report.tiles;
      for (const P of Ie.value) {
        const ee = m.find((H) => H.id === P);
        if (!ee) break;
        n.push(ee), m = ee.tiles ?? [];
      }
      return n;
    });
    ge(C, (n) => {
      n.length !== Ie.value.length && (Ie.value = n.map((m) => m.id));
    });
    const O = g(() => C.value[C.value.length - 1] ?? null), G = g(() => C.value[C.value.length - 2] ?? null), D = g(
      () => O.value ? O.value.tiles ?? [] : a.report.tiles
    ), V = g(() => {
      var n;
      return ((n = O.value) == null ? void 0 : n.id) ?? null;
    }), T = g(() => C.value.length === 0), X = g(() => {
      var n;
      return ((n = O.value) == null ? void 0 : n.tab) === !0;
    }), ae = g(() => {
      var n;
      return ((n = G.value) == null ? void 0 : n.tab) === !0;
    }), Fe = g(
      () => C.value.reduce((n, m) => m.tab ? n : n + 1, 1)
    ), ue = g(() => !X.value && Fe.value < bt), j = g(() => T.value);
    function w(n) {
      n.widget || (Ie.value = [...Ie.value, n.id]);
    }
    function Z() {
      Ie.value = Ie.value.slice(0, -1);
    }
    function ne(n) {
      if (!n) return "";
      const m = `report.widget.types.${n.toLowerCase()}`;
      return b(m) ? v(m) : Le(v, b, n, "other");
    }
    function te(n, m) {
      return n.widget ? n.widget.title ? ne(n.widget.title) : v("report.editor.widget", { index: m + 1 }) : n.tabTitle !== void 0 ? n.tabTitle || v("report.editor.tab", { index: m + 1 }) : n.tab ? v("report.editor.tabGroup") : v("report.editor.group");
    }
    function re(n, m) {
      return !n.widget && X.value ? n.tabTitle || v("report.editor.tab", { index: m + 1 }) : te(n, m);
    }
    function ce(n) {
      var m;
      return ((m = n.tiles) == null ? void 0 : m.length) ?? 0;
    }
    const pe = g(
      () => C.value.map((n, m) => te(n, m)).join(" › ")
    );
    function q(n, m) {
      gt.value = V.value, y.editingTileIndex = m, y.editingTile = JSON.parse(JSON.stringify(n)), B.push({ name: "tileEditor", query: Y.query });
    }
    function L(n, m) {
      D.value.splice(m + 1, 0, Mt(n));
    }
    async function K(n, m) {
      var H;
      if (!await k({
        title: v("report.editor.deleteWidget"),
        message: v("report.editor.deleteWidgetConfirm", { title: re(n, m) })
      })) return;
      const ee = rt(n);
      D.value.splice(m, 1), (H = a.report.globalFilters) == null || H.forEach((A) => {
        var W;
        A.tileIds = (W = A.tileIds) == null ? void 0 : W.filter((se) => !ee.includes(se));
      });
    }
    function ve() {
      O.value && (O.value.tiles || (O.value.tiles = []), O.value.tiles.push(Bt(O.value.tiles.length + 1)));
    }
    function be() {
      D.value.push(Ut());
    }
    function Q() {
      D.value.push(qt());
    }
    function ke(n) {
      var P;
      const m = Number((P = n.size) == null ? void 0 : P.width);
      n.size || (n.size = { width: 12, height: 12 }), !Number.isFinite(m) || m < 1 ? n.size.width = 1 : m > 12 && (n.size.width = 12);
    }
    function ie(n) {
      var P;
      const m = Number((P = n.size) == null ? void 0 : P.height);
      n.size || (n.size = { width: 12, height: 12 }), !Number.isFinite(m) || m < 1 ? n.size.height = 1 : m > 12 && (n.size.height = 12);
    }
    const fe = z(!1), Ve = z(null), Oe = z([]);
    function ye(n) {
      const m = [
        { parentId: null, label: v("report.editor.topLevel"), level: 1 }
      ], P = (H, A, W) => {
        let se = 0;
        H.forEach((de) => {
          var l;
          if (!de.widget)
            if (de.tab) {
              const e = `${W}${v("report.editor.tabGroup")}`;
              (l = de.tiles) == null || l.forEach((s, p) => {
                const F = `${e} › ${s.tabTitle || v("report.editor.tab", { index: p + 1 })}`;
                m.push({ parentId: s.id, label: F, level: A + 1 }), P(s.tiles ?? [], A + 1, `${F} › `);
              });
            } else {
              se += 1;
              const e = `${W}${v("report.editor.group")} ${se}`;
              m.push({ parentId: de.id, label: e, level: A + 1 }), P(de.tiles ?? [], A + 1, `${e} › `);
            }
        });
      };
      P(a.report.tiles, 1, "");
      const ee = new Set(rt(n));
      return m.filter(
        (H) => H.parentId !== V.value && !(H.parentId && ee.has(H.parentId)) && Pt(n, H.level)
      );
    }
    function he(n) {
      Ve.value = n, Oe.value = ye(n), fe.value = !0;
    }
    function Ee(n) {
      const m = Ve.value;
      if (!m) return;
      const P = D.value.indexOf(m);
      P >= 0 && D.value.splice(P, 1), Et(a.report.tiles, n.parentId).push(m), fe.value = !1, Ve.value = null;
    }
    return (n, m) => {
      const P = x("FmButton"), ee = x("FmTextField"), H = x("FmIcon"), A = x("FmMenuItem"), W = x("FmMenu"), se = x("FmList"), de = x("FmListItem"), l = x("FmDialog");
      return u(), R("div", vl, [
        T.value ? U("", !0) : (u(), R("div", fl, [
          h("div", gl, [
            i(P, {
              icon: "arrow_back",
              variant: "tertiary",
              class: "-ml-8",
              onClick: Z
            }),
            h("p", {
              class: "fm-typo-en-body-md-600 line-clamp-1 overflow-hidden break-all min-w-0 grow text-left",
              title: pe.value
            }, E(pe.value), 9, bl)
          ]),
          ae.value && O.value ? (u(), S(ee, {
            key: 0,
            modelValue: O.value.tabTitle,
            "onUpdate:modelValue": m[0] || (m[0] = (e) => O.value.tabTitle = e),
            label: o(v)("report.editor.tabName"),
            placeholder: o(v)("report.editor.tabName")
          }, null, 8, ["modelValue", "label", "placeholder"])) : U("", !0),
          !ae.value && O.value ? (u(), S(ee, {
            key: 1,
            modelValue: O.value.size.width,
            "onUpdate:modelValue": m[1] || (m[1] = (e) => O.value.size.width = e),
            modelModifiers: { number: !0 },
            type: "number",
            label: o(v)("report.editor.width"),
            onBlur: m[2] || (m[2] = (e) => ke(O.value))
          }, null, 8, ["modelValue", "label"])) : U("", !0),
          !ae.value && O.value ? (u(), S(ee, {
            key: 2,
            modelValue: O.value.size.height,
            "onUpdate:modelValue": m[3] || (m[3] = (e) => O.value.size.height = e),
            modelModifiers: { number: !0 },
            type: "number",
            label: o(v)("report.editor.height"),
            onBlur: m[4] || (m[4] = (e) => ie(O.value))
          }, null, 8, ["modelValue", "label"])) : U("", !0)
        ])),
        D.value.length === 0 ? (u(), R("div", yl, E(o(v)("report.editor.emptyLevel")), 1)) : (u(), S(se, { key: 2 }, {
          default: _(() => [
            i(o(ft), {
              list: D.value,
              "item-key": "id",
              class: "flex flex-col gap-2 cursor-grab",
              handle: ".handle"
            }, {
              item: _(({ element: e, index: s }) => [
                h("div", {
                  class: Ue(["flex items-center gap-8 px-12 py-8 border rounded-md border-neutral-300 hover:bg-fm-color-opacity-sm", e.widget ? "cursor-grab" : "cursor-pointer"]),
                  onClick: (p) => w(e)
                }, [
                  i(H, {
                    name: "drag_handle",
                    class: "handle shrink-0 !cursor-grab"
                  }),
                  h("p", {
                    class: "flex-1 min-w-0 truncate text-left fm-typo-en-body-lg-400",
                    title: re(e, s)
                  }, [
                    ze(E(re(e, s)) + " ", 1),
                    e.widget ? U("", !0) : (u(), R("span", wl, " · " + E(ce(e)), 1))
                  ], 8, _l),
                  e.widget ? U("", !0) : (u(), S(H, {
                    key: 0,
                    name: "chevron_right",
                    class: "shrink-0"
                  })),
                  i(W, {
                    trigger: "click",
                    shift: !0,
                    "stop-click-propagation": !0,
                    class: "shrink-0"
                  }, {
                    "menu-button": _(() => [
                      i(P, {
                        "append-icon": "more_vert",
                        variant: "tertiary"
                      })
                    ]),
                    default: _(() => [
                      e.widget ? (u(), S(A, {
                        key: 0,
                        label: o(v)("report.common.edit"),
                        onClick: (p) => q(e, s)
                      }, null, 8, ["label", "onClick"])) : U("", !0),
                      e.tab ? U("", !0) : (u(), S(A, {
                        key: 1,
                        label: o(v)("report.editor.moveTo"),
                        onClick: (p) => he(e)
                      }, null, 8, ["label", "onClick"])),
                      i(A, {
                        label: o(v)("report.common.duplicate"),
                        onClick: (p) => L(e, s)
                      }, null, 8, ["label", "onClick"]),
                      i(A, {
                        onClick: (p) => K(e, s)
                      }, {
                        label: _(() => [
                          h("p", xl, E(o(v)("report.common.remove")), 1)
                        ]),
                        _: 2
                      }, 1032, ["onClick"])
                    ]),
                    _: 2
                  }, 1024)
                ], 10, hl)
              ]),
              _: 1
            }, 8, ["list"])
          ]),
          _: 1
        })),
        X.value ? (u(), S(P, {
          key: 3,
          label: o(v)("report.editor.addTab"),
          variant: "secondary",
          icon: "add",
          "icon-position": "prepend",
          "border-color": o($e).ColorPrimary,
          "text-color": o($e).ColorPrimary,
          onClick: ve
        }, null, 8, ["label", "border-color", "text-color"])) : (u(), S(W, {
          key: 4,
          trigger: "click",
          shift: !0
        }, {
          "menu-button": _(() => [
            i(P, {
              label: o(v)("report.common.add"),
              variant: "secondary",
              icon: "add",
              "icon-position": "prepend",
              class: "w-full",
              "border-color": o($e).ColorPrimary,
              "text-color": o($e).ColorPrimary
            }, null, 8, ["label", "border-color", "text-color"])
          ]),
          default: _(() => [
            i(A, {
              label: o(v)("report.editor.addWidget"),
              onClick: m[5] || (m[5] = (e) => c("add-widget", V.value))
            }, null, 8, ["label"]),
            ue.value ? (u(), S(A, {
              key: 0,
              label: o(v)("report.editor.addGroup"),
              onClick: be
            }, null, 8, ["label"])) : U("", !0),
            j.value ? (u(), S(A, {
              key: 1,
              label: o(v)("report.editor.addTabGroup"),
              onClick: Q
            }, null, 8, ["label"])) : U("", !0)
          ]),
          _: 1
        })),
        i(l, {
          modelValue: fe.value,
          "onUpdate:modelValue": m[6] || (m[6] = (e) => fe.value = e),
          overlay: !0,
          "z-index": 10
        }, {
          "dialog-header": _(() => [
            ze(E(o(v)("report.editor.moveTo")), 1)
          ]),
          default: _(() => [
            i(se, null, {
              default: _(() => [
                (u(!0), R(xe, null, Be(Oe.value, (e) => (u(), S(de, {
                  key: e.parentId ?? "root",
                  onClick: (s) => Ee(e)
                }, {
                  default: _(() => [
                    h("p", Cl, E(e.label), 1)
                  ]),
                  _: 2
                }, 1032, ["onClick"]))), 128))
              ]),
              _: 1
            })
          ]),
          _: 1
        }, 8, ["modelValue"])
      ]);
    };
  }
}), kl = { class: "flex flex-col gap-24 top-0 bg-fm-color-neutral-white z-10" }, Vl = { class: "flex flex-col gap-24 top-0 bg-fm-color-neutral-white z-10" }, Tl = { class: "flex items-center gap-8" }, $l = {
  key: 2,
  class: "flex flex-col gap-8 rounded-md border border-fm-color-border px-16 py-12"
}, Sl = { class: "fm-typo-en-body-sm-400" }, Dl = { class: "fm-typo-en-body-sm-400 font-bold" }, Ol = { class: "space-y-1 fm-typo-en-body-sm-400 text-fm-color-typo-secondary" }, Nl = { class: "flex flex-col gap-24 top-0 bg-fm-color-neutral-white z-10" }, Il = { class: "flex items-center gap-1 max-w-[195px] w-[195px]" }, Rl = { class: "line-clamp-2 overflow-hidden max-w-[195px] w-[195px]" }, mt = /* @__PURE__ */ Ce({
  __name: "ReportEditorTab",
  props: /* @__PURE__ */ Se({
    hideConfiguration: {
      type: Boolean,
      default: !1
    }
  }, {
    modelValue: { required: !0 },
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ Se(["hideConfiguration"], ["update:modelValue"]),
  setup(M, { emit: d }) {
    const a = De(M, "modelValue"), { breakpoints: c } = He(), v = M, b = d, B = ot(), Y = at(), k = z(), {
      currentSelectWidgetType: y,
      categories: C,
      editingTile: O,
      editingTileIndex: G,
      editingGlobalFilter: D,
      editingGlobalFilterIndex: V
    } = Me(lt()), { schemas: T } = Me(zt()), { globalFilters: X } = Me(it()), { getGlobalFilterLabel: ae } = it(), { promptMessage: Fe } = nt(), ue = z(!1), j = z(!1), { t: w, te: Z, locale: ne } = qe(), te = z(void 0), re = g(() => {
      const r = Qe(a.value.tiles).flatMap((t) => {
        var we;
        const f = t.widget.config.schemaNames ?? [], $ = ((we = t.widget.config.joinDatasources) == null ? void 0 : we.flatMap((Ge) => Ge.schemaNames ?? [])) ?? [];
        return [...f, ...$];
      });
      return [...new Set(r)].reduce((t, f) => {
        const $ = T.value.find((we) => we.name === f);
        return $ && t.push($), t;
      }, []);
    }), ce = z(null);
    function pe(r) {
      ce.value = r, j.value = !0;
    }
    ge(
      () => y.value,
      () => {
        y.value !== void 0 && (te.value = jt(y.value), O.value = te.value, G.value = -1, gt.value = ce.value, B.push({
          name: "tileEditor",
          query: Y.query
        }));
      },
      { immediate: !0 }
    ), ge(
      () => a.value.tiles,
      (r) => {
        Lt(r);
      },
      { immediate: !0, deep: !0 }
    );
    const q = [
      {
        label: "report.common.edit",
        selected: !1,
        variant: "tertiary",
        onClick: (r) => {
          var t;
          V.value = r, D.value = JSON.parse(JSON.stringify((t = a.value.globalFilters) == null ? void 0 : t[r])), ue.value = !0;
        }
      },
      {
        label: "report.common.duplicate",
        selected: !1,
        variant: "tertiary",
        onClick: (r) => {
          var $;
          const t = ($ = a.value.globalFilters) == null ? void 0 : $[r];
          if (!t) return;
          const f = JSON.parse(JSON.stringify(t));
          a.value.globalFilters || (a.value.globalFilters = []), a.value.globalFilters.push(f);
        }
      },
      {
        label: "report.common.remove",
        selected: !1,
        variant: "destructive",
        onClick: async (r) => {
          var $, we;
          const t = ($ = a.value.globalFilters) == null ? void 0 : $[r];
          if (!t) return;
          await Fe({
            title: w("report.editor.deleteWidget"),
            message: w("report.editor.deleteWidgetConfirm", {
              title: ae(t)
            })
          }) && ((we = a.value.globalFilters) == null || we.splice(r, 1));
        }
      }
    ], L = g(() => C.value.map((r) => ({
      label: w(`report.category.${r.name}`),
      value: r.id
    }))), { sessionUser: K } = Jt(), ve = g(
      () => {
        var r, t, f, $;
        return ((t = (r = K.value) == null ? void 0 : r.role) == null ? void 0 : t.isAdmin) === !0 || (($ = (f = K.value) == null ? void 0 : f.role) == null ? void 0 : $.isStaff) === !0;
      }
    ), be = g({
      get: () => a.value.queryVersion === "v2",
      set: (r) => {
        a.value.queryVersion = r ? "v2" : "v1", r ? a.value.queryDatasource = a.value.queryDatasource ?? We.Postgres : a.value.queryDatasource = void 0;
      }
    }), Q = [
      { label: w("report.common.postgresql"), value: We.Postgres },
      { label: w("report.common.bigquery"), value: We.BigQuery }
    ], ke = [
      { label: w("report.common.noBatch"), value: "none" },
      { label: w("report.common.days"), value: "days" },
      { label: w("report.common.locations"), value: "locations" }
    ], ie = g({
      get: () => {
        var r, t;
        return (r = a.value.batch) != null && r.days ? "days" : (t = a.value.batch) != null && t.locations ? "locations" : "none";
      },
      set: (r) => {
        var t, f;
        r === "none" ? a.value.batch = void 0 : r === "days" ? a.value.batch = { days: ((t = a.value.batch) == null ? void 0 : t.days) || 1 } : r === "locations" && (a.value.batch = { locations: ((f = a.value.batch) == null ? void 0 : f.locations) || 10 });
      }
    }), fe = g({
      get: () => {
        var r, t;
        if (ie.value === "days") return ((r = a.value.batch) == null ? void 0 : r.days) ?? 1;
        if (ie.value === "locations") return ((t = a.value.batch) == null ? void 0 : t.locations) ?? 1;
      },
      set: (r) => {
        r && (ie.value === "days" ? a.value.batch = { days: r } : ie.value === "locations" && (a.value.batch = { locations: r }));
      }
    }), Ve = g({
      get: () => Le(w, Z, a.value.name, "other"),
      set: (r) => {
        a.value.name = r;
      }
    }), { country: Oe } = Me(Kt()), {
      dateRange: ye,
      compareDateRange: he,
      timeRange: Ee,
      selectedLocationIds: n,
      businessDataVersion: m,
      restaurants: P,
      warehouses: ee
    } = Me(Qt()), H = g(
      () => a.value.queryVersion === "v2" && a.value.queryDatasource === We.BigQuery
    ), A = g(() => H.value && ve.value), W = z(null), se = z(!1), de = g(() => {
      const r = ye.value.startDate ?? ye.value.endDate;
      return r ? {
        startDate: r,
        endDate: r
      } : ye.value;
    }), l = g(() => {
      const r = he.value.startDate ?? he.value.endDate;
      return r ? {
        startDate: r,
        endDate: r
      } : he.value;
    }), e = g(() => {
      const r = a.value.batch;
      if (!r) return 1;
      if (r.locations) {
        const t = P.value.length + ee.value.length;
        return Math.max(1, Math.ceil(t / r.locations));
      }
      return 1;
    }), s = g(() => {
      if (W.value == null || W.value === 0) return null;
      const r = W.value * e.value;
      return ut(r, Oe.value);
    }), p = g(() => W.value == null || W.value === 0 ? null : ut(W.value, Oe.value)), F = g(() => s.value ? dt(s.value.amount, s.value.symbol) : ""), I = g(() => p.value ? dt(p.value.amount, p.value.symbol) : ""), J = g(() => e.value === 1 ? w("report.common.estimatedCostBatchSingular") : w("report.common.estimatedCostBatchPlural")), le = g(() => se.value ? "..." : !F.value || !I.value ? "--" : w("report.common.estimatedCostFormula", {
      batchCount: e.value,
      batchLabel: J.value,
      baseCost: I.value,
      totalCost: F.value
    }));
    let me = [];
    const _e = Yt(async () => {
      if (!A.value) return;
      const r = Qe(a.value.tiles).filter(
        (t) => {
          var f;
          return ((f = t.widget.config.schemaNames) == null ? void 0 : f.length) > 0;
        }
      );
      if (r.length === 0) {
        W.value = null;
        return;
      }
      se.value = !0, me.forEach((t) => t.abort()), me = [];
      try {
        const t = await Promise.all(
          r.map((f) => {
            const $ = new AbortController();
            return me.push($), Gt({
              widget: f.widget,
              signal: $.signal,
              tileId: f.id,
              scopedDateRange: de.value,
              scopedCompareDateRange: l.value
            });
          })
        );
        W.value = t.reduce((f, $) => f + ($.totalBytesProcessed ?? 0), 0);
      } catch (t) {
        t.code !== "ERR_CANCELED" && (W.value = null);
      } finally {
        se.value = !1;
      }
    }, 1e3);
    ge(
      [
        () => a.value.tiles,
        () => a.value.queryVersion,
        () => a.value.queryDatasource,
        () => a.value.batch,
        () => ye.value,
        () => Ee.value,
        () => he.value,
        () => n.value,
        () => m.value
      ],
      () => {
        _e();
      },
      { deep: !0 }
    ), pt(() => {
      _e();
    }), Vt(() => {
      me.forEach((r) => r.abort());
    });
    function Ne(r) {
      var t;
      return !((t = r.tileIds) != null && t.length);
    }
    function Pe(r) {
      return !re.value.find((t) => t.name === r.schemaName);
    }
    return ge(
      () => a.value.globalFilters,
      (r) => {
        X.value = JSON.parse(JSON.stringify(r || []));
      },
      { deep: !0, immediate: !0 }
    ), (r, t) => {
      const f = x("FmTextField"), $ = x("FmSelect"), we = x("FmSwitch"), Ge = x("FmButton"), je = x("FmIcon"), Je = x("FmTooltip"), ht = x("FmMenuItem"), _t = x("FmMenu"), wt = x("FmListItem"), xt = x("FmList"), Ct = x("FmForm");
      return u(), S(Tt(o(c).lg || o(c).md ? el : tl), {
        "model-value": v.hideConfiguration,
        "onUpdate:modelValue": t[12] || (t[12] = (N) => b("hideConfiguration", N))
      }, {
        default: _(() => [
          i(ul, {
            "is-open": ue.value,
            "onUpdate:isOpen": t[0] || (t[0] = (N) => ue.value = N),
            "global-filter": o(D),
            tiles: o(Qe)(a.value.tiles),
            schemas: re.value,
            "onUpdate:globalFilter": t[1] || (t[1] = (N) => {
              N && (a.value.globalFilters || (a.value.globalFilters = []), typeof o(V) == "number" && o(V) >= 0 ? a.value.globalFilters[o(V)] = N : a.value.globalFilters.push(N));
            })
          }, null, 8, ["is-open", "global-filter", "tiles", "schemas"]),
          i(Xt, {
            modelValue: o(y),
            "onUpdate:modelValue": t[2] || (t[2] = (N) => vt(y) ? y.value = N : null),
            "is-open": j.value,
            "onUpdate:isOpen": t[3] || (t[3] = (N) => j.value = N)
          }, null, 8, ["modelValue", "is-open"]),
          i(Ct, {
            ref_key: "form",
            ref: k,
            class: "px-6 py-16 flex flex-col gap-24 overflow-y-auto overflow-x-hidden"
          }, {
            default: _(() => [
              i(Ae, { class: "border-solid" }, {
                header: _(() => [
                  h("p", null, E(o(w)("report.editor.reportInfo")), 1),
                  t[13] || (t[13] = h("div", { class: "grow" }, null, -1))
                ]),
                content: _(() => [
                  h("div", kl, [
                    i(f, {
                      modelValue: Ve.value,
                      "onUpdate:modelValue": t[4] || (t[4] = (N) => Ve.value = N),
                      label: o(w)("report.common.name"),
                      placeholder: o(w)("report.common.name")
                    }, null, 8, ["modelValue", "label", "placeholder"]),
                    (u(), S($, {
                      key: `category-${o(ne)}`,
                      modelValue: a.value.category,
                      "onUpdate:modelValue": t[5] || (t[5] = (N) => a.value.category = N),
                      label: o(w)("report.common.category"),
                      items: L.value
                    }, null, 8, ["modelValue", "label", "items"])),
                    i(f, {
                      modelValue: a.value.description,
                      "onUpdate:modelValue": t[6] || (t[6] = (N) => a.value.description = N),
                      label: o(w)("report.common.description"),
                      placeholder: o(w)("report.common.description")
                    }, null, 8, ["modelValue", "label", "placeholder"])
                  ])
                ]),
                _: 1
              }),
              i(Ae, {
                class: "border-solid",
                "is-open": !1
              }, {
                header: _(() => [
                  h("p", null, E(o(w)("report.editor.advancedUsage")), 1),
                  t[14] || (t[14] = h("div", { class: "grow" }, null, -1))
                ]),
                content: _(() => [
                  h("div", Vl, [
                    h("div", Tl, [
                      t[15] || (t[15] = h("span", { class: "fm-typo-en-body-sm-400 text-fm-color-typo-secondary" }, "V2", -1)),
                      i(we, {
                        modelValue: be.value,
                        "onUpdate:modelValue": t[7] || (t[7] = (N) => be.value = N)
                      }, null, 8, ["modelValue"])
                    ]),
                    be.value && ve.value ? (u(), S($, {
                      key: 0,
                      modelValue: a.value.queryDatasource,
                      "onUpdate:modelValue": t[8] || (t[8] = (N) => a.value.queryDatasource = N),
                      label: o(w)("report.common.queryDatasource"),
                      items: Q
                    }, null, 8, ["modelValue", "label"])) : U("", !0),
                    i($, {
                      modelValue: ie.value,
                      "onUpdate:modelValue": t[9] || (t[9] = (N) => ie.value = N),
                      label: o(w)("report.common.batch"),
                      items: ke
                    }, null, 8, ["modelValue", "label"]),
                    ie.value !== "none" ? (u(), S(f, {
                      key: 1,
                      modelValue: fe.value,
                      "onUpdate:modelValue": t[10] || (t[10] = (N) => fe.value = N),
                      modelModifiers: { number: !0 },
                      label: ie.value === "days" ? o(w)("report.common.days") : o(w)("report.common.locations"),
                      type: "number"
                    }, null, 8, ["modelValue", "label"])) : U("", !0),
                    A.value ? (u(), R("div", $l, [
                      h("span", Sl, E(o(w)("report.common.estimatedBqCost")), 1),
                      h("p", Dl, E(le.value), 1),
                      h("div", Ol, [
                        h("div", null, "* " + E(o(w)("report.common.estimatedCostGuidelineBigQuery")), 1),
                        h("div", null, "* " + E(o(w)("report.common.estimatedCostGuidelineDateRange")), 1),
                        h("div", null, "* " + E(o(w)("report.common.estimatedCostGuidelineLocationBatch")), 1)
                      ])
                    ])) : U("", !0)
                  ])
                ]),
                _: 1
              }),
              i(Ae, {
                class: "border-solid",
                "is-open": !1
              }, {
                header: _(() => [
                  h("p", null, E(o(w)("report.section.filter.globalFilter")), 1),
                  t[16] || (t[16] = h("div", { class: "grow" }, null, -1))
                ]),
                content: _(() => [
                  h("div", Nl, [
                    i(Ge, {
                      label: o(w)("report.section.filter.addGlobalFilter"),
                      variant: "secondary",
                      icon: "add",
                      "icon-position": "prepend",
                      "border-color": o($e).ColorPrimary,
                      "text-color": o($e).ColorPrimary,
                      onClick: t[11] || (t[11] = () => {
                        V.value = null, D.value = null, ue.value = !0;
                      })
                    }, null, 8, ["label", "border-color", "text-color"]),
                    i(xt, null, {
                      default: _(() => [
                        i(o(ft), {
                          list: a.value.globalFilters,
                          "item-key": "`${element.schemaName}-${element.name}`",
                          class: "flex flex-col gap-2 cursor-grab",
                          handle: ".handle"
                        }, {
                          item: _(({ element: N, index: Ft }) => [
                            i(wt, { class: "!cursor-grab border rounded-md border-neutral-300" }, {
                              default: _(() => [
                                i(je, {
                                  name: "drag_handle",
                                  class: "handle"
                                }),
                                h("div", Il, [
                                  i(Je, {
                                    "z-index": 99,
                                    content: o(ae)(N),
                                    class: "handle"
                                  }, {
                                    default: _(() => [
                                      h("p", Rl, E(o(ae)(N)), 1)
                                    ]),
                                    _: 2
                                  }, 1032, ["content"]),
                                  Pe(N) ? (u(), S(Je, {
                                    key: 0,
                                    content: o(w)("report.section.filter.missingSchema"),
                                    "z-index": 99
                                  }, {
                                    default: _(() => [
                                      i(je, {
                                        name: "warning",
                                        class: "text-fm-color-system-error-300"
                                      })
                                    ]),
                                    _: 1
                                  }, 8, ["content"])) : Ne(N) ? (u(), S(Je, {
                                    key: 1,
                                    content: o(w)("report.section.filter.missingTileIds"),
                                    "z-index": 99
                                  }, {
                                    default: _(() => [
                                      i(je, {
                                        name: "warning",
                                        class: "text-fm-color-system-warning-300"
                                      })
                                    ]),
                                    _: 1
                                  }, 8, ["content"])) : U("", !0)
                                ]),
                                t[17] || (t[17] = h("div", { class: "grow" }, null, -1)),
                                i(_t, {
                                  trigger: "click",
                                  shift: !0,
                                  "stop-click-propagation": !0
                                }, {
                                  "menu-button": _(() => [
                                    i(Ge, {
                                      "append-icon": "more_vert",
                                      variant: "tertiary"
                                    })
                                  ]),
                                  default: _(() => [
                                    (u(), R(xe, null, Be(q, (Ke, kt) => i(ht, {
                                      key: kt,
                                      onClick: (Ql) => Ke.onClick(Ft)
                                    }, {
                                      label: _(() => [
                                        h("p", {
                                          class: Ue(
                                            Ke.variant === "destructive" ? " text-fm-color-system-error-300" : ""
                                          )
                                        }, E(o(w)(Ke.label)), 3)
                                      ]),
                                      _: 2
                                    }, 1032, ["onClick"])), 64))
                                  ]),
                                  _: 2
                                }, 1024)
                              ]),
                              _: 2
                            }, 1024)
                          ]),
                          _: 1
                        }, 8, ["list"])
                      ]),
                      _: 1
                    })
                  ])
                ]),
                _: 1
              }),
              i(Ae, { class: "border-solid" }, {
                header: _(() => [
                  h("p", null, E(o(w)("report.editor.widgets")), 1),
                  t[18] || (t[18] = h("div", { class: "grow" }, null, -1))
                ]),
                content: _(() => [
                  i(Fl, {
                    report: a.value,
                    onAddWidget: pe
                  }, null, 8, ["report"])
                ]),
                _: 1
              })
            ]),
            _: 1
          }, 512)
        ]),
        _: 1
      }, 8, ["model-value"]);
    };
  }
}), Ml = { class: "h-full w-full overflow-x-hidden" }, Bl = { class: "flex flex-col bg-fm-color-neutral-white w-[400px]" }, Ul = { class: "flex bg-fm-color-neutral-white w-[88px]" }, ql = {
  key: 1,
  class: "w-full overflow-y-auto"
}, El = {
  key: 0,
  class: "w-full grid grid-cols-12 gap-[24px] p-24"
}, Pl = { class: "col-span-12 mb-4 flex flex-wrap gap-2 pb-2" }, zl = { key: 2 }, Ll = {
  key: 0,
  class: "grid grid-cols-12 gap-[16px]"
}, Gl = { class: "col-span-12 mb-4 flex flex-wrap gap-2 pb-2" }, Al = {
  key: 3,
  class: "h-full w-full flex items-start justify-center bg-fm-color-neutral-white py-24",
  style: { backgroundColor: "#F6F8FC" }
}, Wl = {
  key: 0,
  class: "w-full grid grid-cols-12 gap-[24px] p-24"
}, Hl = /* @__PURE__ */ Ce({
  __name: "ReportEditorViewport",
  props: /* @__PURE__ */ Se({
    hideConfiguration: { type: Boolean, required: !1, default: !1 },
    previewWidth: { type: Number, required: !0 },
    previewHeight: { type: Number, required: !0 },
    previewScale: { type: Number, required: !0 }
  }, {
    modelValue: { required: !0, default: () => {
    } },
    modelModifiers: {}
  }),
  emits: /* @__PURE__ */ Se(["hideConfiguration"], ["update:modelValue"]),
  setup(M, { emit: d }) {
    const { breakpoints: a } = He(), c = De(M, "modelValue"), v = g(() => {
      var y;
      return At((y = c.value) == null ? void 0 : y.tiles);
    }), b = M, B = g(() => b.previewWidth > 0 && b.previewHeight > 0), Y = g(() => a.value.sm || a.value.xs), k = d;
    return (y, C) => {
      const O = x("FmCircularProgress"), G = x("FmButton"), D = x("FmMenuDivider"), V = x("FmCard");
      return u(), R("div", Ml, [
        c.value ? (u(), R(xe, { key: 1 }, [
          B.value ? U("", !0) : (u(), S(Nt, {
            key: 0,
            "model-value": !b.hideConfiguration
          }, {
            right: _(() => [
              h("div", Bl, [
                c.value ? (u(), S(mt, {
                  key: 0,
                  modelValue: c.value,
                  "onUpdate:modelValue": C[0] || (C[0] = (T) => c.value = T),
                  "hide-configuration": b.hideConfiguration,
                  onHideConfiguration: C[1] || (C[1] = (T) => k("hideConfiguration", T))
                }, null, 8, ["modelValue", "hide-configuration"])) : U("", !0)
              ])
            ]),
            left: _(() => [
              h("div", Ul, [
                i(G, {
                  variant: "secondary",
                  icon: "keyboard_double_arrow_right",
                  "border-color": o($e).ColorPrimary,
                  "text-color": o($e).ColorPrimary,
                  class: "m-24 my-16",
                  size: "md",
                  onClick: C[2] || (C[2] = (T) => k("hideConfiguration", !b.hideConfiguration))
                }, null, 8, ["border-color", "text-color"])
              ])
            ]),
            _: 1
          }, 8, ["model-value"])),
          i(D, {
            vertical: "",
            style: { margin: "0px" }
          }),
          !Y.value && !B.value ? (u(), R("div", ql, [
            c.value.tiles.length > 0 ? (u(), R("div", El, [
              h("div", Pl, [
                i(st)
              ]),
              i(Ye, {
                tiles: c.value.tiles,
                "report-id": c.value.id || "editing",
                "leaf-index-by-id": v.value,
                "hide-export": !0
              }, null, 8, ["tiles", "report-id", "leaf-index-by-id"])
            ])) : (u(), S(Ze, { key: 1 }))
          ])) : Y.value && !B.value ? (u(), R("div", zl, [
            i(V, { class: "px-5 pb-28" }, {
              default: _(() => [
                c.value.tiles.length > 0 ? (u(), R("div", Ll, [
                  h("div", Gl, [
                    i(st)
                  ]),
                  i(Ye, {
                    tiles: c.value.tiles,
                    "report-id": c.value.id || "editing",
                    "leaf-index-by-id": v.value,
                    "hide-export": !0
                  }, null, 8, ["tiles", "report-id", "leaf-index-by-id"])
                ])) : (u(), S(Ze, { key: 1 }))
              ]),
              _: 1
            }),
            c.value ? (u(), S(mt, {
              key: 0,
              modelValue: c.value,
              "onUpdate:modelValue": C[3] || (C[3] = (T) => c.value = T),
              "hide-configuration": b.hideConfiguration,
              onHideConfiguration: C[4] || (C[4] = (T) => {
                k("hideConfiguration", T);
              })
            }, null, 8, ["modelValue", "hide-configuration"])) : U("", !0)
          ])) : (u(), R("div", Al, [
            h("div", {
              style: et({
                transformOrigin: "top center",
                transform: `scale(${b.previewScale})`
              })
            }, [
              h("div", {
                class: "overflow-y-auto bg-white rounded-lg",
                style: et({
                  width: b.previewWidth + "px",
                  height: b.previewHeight + "px"
                })
              }, [
                c.value.tiles.length > 0 ? (u(), R("div", Wl, [
                  i(Ye, {
                    tiles: c.value.tiles,
                    "report-id": c.value.id || "editing",
                    "leaf-index-by-id": v.value,
                    "hide-export": !0,
                    "custom-screen-width": b.previewWidth
                  }, null, 8, ["tiles", "report-id", "leaf-index-by-id", "custom-screen-width"])
                ])) : (u(), S(Ze, { key: 1 }))
              ], 4)
            ], 4)
          ]))
        ], 64)) : (u(), S(O, { key: 0 }))
      ]);
    };
  }
}), jl = { class: "flex items-center gap-4" }, Jl = { class: "flex justify-center items-center gap-2 w-full" }, Kl = { class: "flex gap-4" }, po = /* @__PURE__ */ Ce({
  __name: "ReportEditor",
  setup(M) {
    const d = ot(), a = at(), c = lt(), { promptLoader: v } = Ht(), { t: b, te: B } = qe(), { currentReport: Y, editingReport: k } = Me(c), { breakpoints: y } = He(), C = g(() => {
      var q;
      return !((q = k.value) != null && q.editable);
    }), O = g(() => {
      var q;
      return ((q = k.value) == null ? void 0 : q.name) ?? b("report.editor.newReport");
    }), G = g(() => Le(b, B, O.value, "other")), D = yt(), { promptMessage: V } = nt(), T = z(!1), X = g(() => [
      { label: b("report.common.cancel"), value: "cancel", isPrimary: !1 },
      { label: b("report.common.save"), value: "save", isPrimary: !0, prependIcon: "download" }
    ]);
    async function ae() {
      if (!k.value || k.value.tiles.length === 0) {
        D.open({ message: b("report.messages.noWidget"), type: "error" });
        return;
      }
      if (Wt(k.value.tiles) > bt) {
        D.open({ message: b("report.messages.tooDeepNesting"), type: "error" });
        return;
      }
      const q = C.value ? b("report.common.clone") : b("report.common.save");
      if (await V({
        title: `${b("report.messages.saveReport")}`,
        message: b("report.messages.confirmAction", { action: q, name: G.value })
      }))
        try {
          await v(async () => {
            if (C.value) {
              const K = JSON.parse(JSON.stringify(k.value));
              K.editable = !0, K.id = "", K.name = `${K.name} ${b("report.common.duplicate")}`, K.isDefault = !1, await c.createOrUpdateReport(K);
            } else
              k.value && await c.createOrUpdateReport(k.value);
            d.push({
              name: "reportView",
              query: a.query
            });
          });
        } catch (K) {
          console.log(K), D.open({
            message: b("report.messages.failedToAction", { action: q }),
            type: "error"
          });
        }
    }
    async function Fe() {
      var L;
      if (!k.value) return;
      await V({
        title: b("report.common.report", 1),
        message: b("report.editor.notSaved", { name: (L = k.value) == null ? void 0 : L.name })
      }) && (k.value = JSON.parse(JSON.stringify(Y.value)), d.back());
    }
    pt(() => {
      k.value || d.push({ name: "reports" });
    });
    const ue = [
      {
        labelKey: "report.common.default",
        width: void 0,
        height: void 0
      },
      {
        labelKey: "report.editor.devices.desktop",
        width: 1920,
        height: 1080
      },
      {
        labelKey: "report.editor.devices.tablet",
        width: 820,
        height: 1180
      },
      {
        labelKey: "report.editor.devices.phone",
        width: 390,
        height: 859
      }
    ], j = z(ue[0]), w = z(!1), Z = g(() => w.value ? Math.max(j.value.width || 0, j.value.height || 0) : Math.min(j.value.width || 0, j.value.height || 0)), ne = g(() => w.value ? Math.min(j.value.width || 0, j.value.height || 0) : Math.max(j.value.width || 0, j.value.height || 0)), te = g(() => window.innerHeight - 72 - 56), re = g(() => {
      if (!Z.value) return 1;
      let q = (te.value - 48) / Z.value, L = ne.value * q;
      return L > window.innerWidth && (q = window.innerWidth / ne.value, L = ne.value * q), q > 1 ? 1 : q;
    });
    function ce(q) {
      j.value = q;
    }
    function pe() {
      w.value = !w.value;
    }
    return (q, L) => {
      const K = x("FmTooltip"), ve = x("FmButton"), be = x("FmPageHead");
      return u(), S(It, null, {
        default: _(() => [
          i(be, {
            "data-editor-header": "true",
            actions: X.value,
            "onClick:action": L[0] || (L[0] = (Q) => {
              Q === "cancel" && Fe(), Q === "save" && ae();
            })
          }, {
            title: _(() => [
              h("div", jl, [
                i(K, {
                  content: G.value,
                  placement: "bottom"
                }, {
                  default: _(() => [
                    h("p", {
                      class: Ue(`${o(y).sm || o(y).xs ? "hidden" : ""} text-ellipsis overflow-hidden text-nowrap fm-typo-en-title-md-600 w-[250px]`)
                    }, E(G.value), 3)
                  ]),
                  _: 1
                }, 8, ["content"]),
                h("div", Jl, [
                  h("div", Kl, [
                    (u(), R(xe, null, Be(ue, (Q) => {
                      var ke;
                      return i(ve, {
                        key: Q.labelKey,
                        label: o(b)(Q.labelKey),
                        variant: ((ke = j.value) == null ? void 0 : ke.labelKey) === Q.labelKey ? "primary" : "secondary",
                        size: "md",
                        onClick: (ie) => ce(Q)
                      }, null, 8, ["label", "variant", "onClick"]);
                    }), 64))
                  ]),
                  i(ve, {
                    icon: "screen_rotation",
                    variant: "tertiary",
                    size: "md",
                    class: "text-fm-color-typo-primary",
                    onClick: pe
                  })
                ])
              ])
            ]),
            _: 1
          }, 8, ["actions"]),
          o(k) ? (u(), S(Hl, {
            key: 0,
            modelValue: o(k),
            "onUpdate:modelValue": L[1] || (L[1] = (Q) => vt(k) ? k.value = Q : null),
            style: et({ height: te.value + "px" }),
            class: Ue(o(y).lg || o(y).md ? "flex" : ""),
            "hide-configuration": T.value,
            "preview-width": ne.value,
            "preview-height": Z.value,
            "preview-scale": re.value,
            onHideConfiguration: L[2] || (L[2] = (Q) => T.value = Q)
          }, null, 8, ["modelValue", "style", "class", "hide-configuration", "preview-width", "preview-height", "preview-scale"])) : U("", !0)
        ]),
        _: 1
      });
    };
  }
});
export {
  po as default
};
