import { defineComponent as te, ref as y, computed as p, watch as J, onMounted as re, onUnmounted as ae, resolveComponent as ne, createBlock as k, openBlock as d, withCtx as se, createVNode as B, createElementVNode as N, unref as a, normalizeStyle as le, normalizeClass as T, createElementBlock as q, Fragment as ie, renderList as ue } from "vue";
import { _ as ce } from "./MenuTab.vue_vue_type_script_setup_true_lang-Dr7w9A6f.js";
import { _ as pe } from "./FilterSelector.vue_vue_type_script_setup_true_lang-CIh5MO0J.js";
import { useRouter as me, useRoute as de } from "vue-router";
import { _ as ve } from "./Default.vue_vue_type_script_setup_true_lang-CNXchelN.js";
import { a as fe, s as ge } from "./BaseDialog.vue_vue_type_script_setup_true_lang-Dmpc24i4.js";
import { storeToRefs as b } from "pinia";
import { b as ye, c as be, u as _e, g as we } from "./NavigationTab.vue_vue_type_script_setup_true_lang-Dbo0pwH4.js";
import { _ as Se } from "./NoData.vue_vue_type_script_setup_true_lang-BwIUTmvY.js";
import { useSnackbar as ke, useBreakpoints as he } from "@feedmepos/ui-library";
import { u as Ce, e as xe, d as Re } from "./vue-i18n-C1hP_yGq.js";
import { u as Oe } from "./message-dialog-UoMWOnil.js";
import { r as Pe } from "./i18n-CI_sQ5d_.js";
import { _ as De } from "./SelectComponent.vue_vue_type_script_setup_true_lang-BEm9iVWM.js";
const Ie = /* @__PURE__ */ te({
  __name: "ReportView",
  setup($e) {
    const { t: o, te: L } = Ce(), _ = me(), v = de(), i = ke(), { promptMessage: V } = Oe(), m = fe(), h = ye(), { promptLoader: w } = xe(), { breakpoints: s } = he(), { isOpenAccountingDialog: j } = b(be()), { isOpenScheduleDialog: C } = b(_e()), { disabled: x } = b(Re()), f = y(null), F = y(0), R = y(0);
    let u = null;
    const O = y(null), P = p(() => {
      var e;
      return ((e = O.value) == null ? void 0 : e.reportId) === c.value;
    }), H = p(
      () => {
        var e;
        return !!((e = t.value) != null && e.isSupportPos);
      }
    ), {
      currentReport: t,
      overviewReport: M,
      currentReportId: c,
      isOpenDownloadDialog: U,
      isOpenEmailDialog: Y,
      editingReport: z,
      canCreateCustomReport: G,
      customReportQuota: D
    } = b(m);
    J(f, (e) => {
      u == null || u.disconnect(), e && (u = new ResizeObserver(() => {
        S();
      }), u.observe(e), S());
    }), J(
      () => c.value,
      async () => O.value = await we(c.value),
      { immediate: !0 }
    ), re(() => {
      S();
    }), ae(() => {
      u == null || u.disconnect();
    });
    function S() {
      if (f.value) {
        const e = f.value.getBoundingClientRect();
        F.value = e.height, R.value = e.bottom;
      }
    }
    const Q = p(() => s.value.xs || s.value.sm ? 1 : R.value), W = p(() => {
      var l;
      const e = c.value === ((l = M.value) == null ? void 0 : l.id), r = [
        {
          icon: "mail",
          label: o("report.common.emailSchedule"),
          onClick: () => {
            C.value = !0;
          }
        },
        {
          icon: "article",
          label: o(e ? "report.common.unsetAsOverview" : "report.common.setAsOverview"),
          onClick: async () => {
            await w(async () => {
              if (t.value)
                try {
                  await m.updateDefaultReport(e ? null : t.value.id), i.open({
                    message: `Successfully ${e ? "unset" : "set"} ${t.value.name} as Overview`,
                    type: "success"
                  });
                } catch (g) {
                  i.open({ message: JSON.stringify(g), type: "error" });
                }
            });
          }
        },
        {
          icon: "content_copy",
          label: o("report.common.copyID"),
          onClick: () => {
            t.value && (navigator.clipboard.writeText(c.value), i.open({ message: o("report.messages.copiedReportIdToClipboard"), type: "success" }));
          }
        },
        {
          icon: "report_jobs",
          label: o("report.common.reportStatus"),
          onClick: () => {
            I();
          }
        }
      ];
      return !x.value && P.value && r.splice(1, 0, {
        icon: "download",
        label: o("report.common.accountingExport"),
        onClick: $
      }), r;
    });
    async function I() {
      h.isOpenJobDialog = !0, await h.getJobs();
    }
    function K() {
      z.value = JSON.parse(JSON.stringify(t.value)), _.push({
        name: "reportEditor",
        query: v.query
      });
    }
    async function X() {
      if (!G.value) {
        let r = `Your current plan only support up to ${D.value} custom reports.`;
        D.value === 0 && (r = "Your current plan does not support custom report."), i.open({
          message: r,
          type: "error"
        });
        return;
      }
      if (!t.value) {
        i.open({ message: "No Widget", type: "error" });
        return;
      }
      if (await V({
        title: `${o("report.messages.saveReport")}`,
        message: o("report.messages.confirmAction", {
          action: o("report.common.duplicate"),
          name: t.value.name
        })
      }) && t.value) {
        const r = JSON.parse(JSON.stringify(t.value));
        r.editable = !0, r.id = "", r.name = `${r.name} duplicate`;
        try {
          await w(async () => {
            const l = await m.createOrUpdateReport(r);
            m.updateCurrentReportId(l.id), _.push({
              name: "reportView",
              params: v.params,
              query: v.query
            });
          });
        } catch (l) {
          console.log(l), i.open({ message: "Failed to clone report", type: "error" });
        }
      }
    }
    function $() {
      j.value = !0;
    }
    function Z() {
      t.value && (navigator.clipboard.writeText(c.value), i.open({ message: "Copied report ID to clipboard", type: "success" }));
    }
    async function ee() {
      if (t.value)
        try {
          await w(async () => {
            await ge(c.value, !H.value), await m.getReports();
          });
        } catch (e) {
          console.log(e), i.open({ message: o("report.messages.failedToAction", { action: o("report.common.update") }), type: "error" });
        }
    }
    const E = p(() => t.value ? t.value.tiles : []), oe = p(() => {
      var r;
      let e = [
        // TODO: In future once decided to support report showing configuration for POS, 
        // uncomment this:
        // {
        //   label: t("report.common.showInPOS"),
        //   value: "show_in_pos",
        //   isPrimary: false,
        //   appendIcon: isCurrentReportShownInPOS.value
        //     ? "radio_button_checked"
        //     : "radio_button_unchecked"
        // },
        (r = t == null ? void 0 : t.value) != null && r.editable ? { label: o("report.common.edit"), value: "edit", isPrimary: !1 } : {
          label: o("report.common.duplicate"),
          value: "duplicate",
          isPrimary: !1
        },
        {
          label: o("report.common.download"),
          value: "download",
          isPrimary: !0,
          prependIcon: "download"
        },
        { label: o("report.schedule.sendEmail"), value: "email", isPrimary: !0 }
      ];
      return s.value.xs && (e = [
        ...e,
        { label: o("report.schedule.sendEmail"), value: "email", isPrimary: !1 },
        { label: o("report.common.reportStatus"), value: "report_jobs", isPrimary: !1 },
        { label: o("report.common.emailSchedule"), value: "schedule_email", isPrimary: !1 },
        { label: o("report.common.copyID"), value: "content_copy", isPrimary: !1 }
      ], !x.value && P.value && e.push({ label: o("report.common.accountingExport"), value: "export", isPrimary: !1 }), e = e.filter((l) => l.value !== "edit")), e;
    });
    return (e, r) => {
      const l = ne("FmPageHead");
      return d(), k(ve, null, {
        default: se(() => {
          var g;
          return [
            B(l, {
              title: a(Pe)(a(o), a(L), (g = a(t)) == null ? void 0 : g.name, "other"),
              "back-button": !0,
              actions: oe.value,
              "onClick:back": r[0] || (r[0] = (n) => a(_).push({ name: "reports", query: a(v).query })),
              "onClick:action": r[1] || (r[1] = (n) => {
                n === "show_in_pos" && ee(), n === "duplicate" && X(), n === "download" && (U.value = !0), n === "email" && (Y.value = !0), n === "edit" && K(), n === "schedule_email" && (C.value = !0), n === "content_copy" && Z(), n === "export" && $(), n === "report_jobs" && I();
              }),
              class: "remove-line-clamping"
            }, null, 8, ["title", "actions"]),
            N("div", {
              class: T(`flex flex-col overflow-y-auto ${a(s).xs || a(s).sm ? "pb-20" : ""}`),
              style: le({ height: `calc(100% - ${a(s).lg || a(s).md ? 72 : 64}px)` })
            }, [
              N("div", {
                ref_key: "menuTabRef",
                ref: f,
                class: "sticky top-0 z-10 bg-white"
              }, [
                B(ce, {
                  "model-value": a(s).lg || a(s).md ? W.value : []
                }, null, 8, ["model-value"])
              ], 512),
              a(t) && E.value.length > 0 ? (d(), q("div", {
                key: 0,
                class: T(["grid grid-cols-12", a(s).sm || a(s).xs ? "px-3 gap-[16px]" : "px-24 gap-[24px]"])
              }, [
                (d(!0), q(ie, null, ue(E.value, (n, A) => (d(), k(pe, {
                  key: `widget_${a(c)}_${A}`,
                  "model-value": n,
                  index: A,
                  "sticky-header-offset": Q.value
                }, null, 8, ["model-value", "index", "sticky-header-offset"]))), 128))
              ], 2)) : (d(), k(Se, { key: 1 }))
            ], 6)
          ];
        }),
        _: 1
      });
    };
  }
}), Ye = /* @__PURE__ */ De(Ie, [["__scopeId", "data-v-57161c29"]]);
export {
  Ye as default
};
