import { defineComponent as z, computed as v, resolveComponent as H, createBlock as g, openBlock as u, withCtx as M, createVNode as h, createElementVNode as D, unref as o, normalizeStyle as Y, normalizeClass as R, createElementBlock as $, Fragment as G, renderList as Q } from "vue";
import { _ as U } from "./MenuTab.vue_vue_type_script_setup_true_lang-D0UI760k.js";
import { _ as W } from "./DynamicWidget.vue_vue_type_script_setup_true_lang-FhZDnXkv.js";
import { useRouter as K, useRoute as X } from "vue-router";
import { _ as Z } from "./Default.vue_vue_type_script_setup_true_lang-BuLc4McY.js";
import { a as ee } from "./BaseDialog.vue_vue_type_script_setup_true_lang-BqtS2XG4.js";
import { storeToRefs as p } from "pinia";
import { b as oe, c as te, u as re } from "./NavigationTab.vue_vue_type_script_setup_true_lang-EbtoF2G2.js";
import { _ as ae } from "./NoData.vue_vue_type_script_setup_true_lang-j8BlZPPp.js";
import { useSnackbar as le, useBreakpoints as ne } from "@feedmepos/ui-library";
import { u as ie, f as se, e as ue } from "./vue-i18n-DG0DIzwC.js";
import { u as ce } from "./message-dialog-UoMWOnil.js";
import { r as pe } from "./i18n-Cif1I9J2.js";
const me = { class: "sticky top-0 z-10 bg-white" }, Re = /* @__PURE__ */ z({
  __name: "ReportView",
  setup(de) {
    const { t: r, te: P } = ie(), m = K(), d = X(), i = le(), { promptMessage: E } = ce(), f = ee(), b = oe(), { promptLoader: I } = se(), { breakpoints: n } = ne(), {
      currentReport: t,
      currentReportId: c,
      isOpenDownloadDialog: O,
      isOpenEmailDialog: J,
      editingReport: N,
      canCreateCustomReport: q,
      customReportQuota: y
    } = p(f), { isOpenAccountingDialog: A } = p(te()), { isOpenScheduleDialog: _ } = p(re()), { disabled: B } = p(ue()), L = v(() => {
      const l = [
        {
          icon: "mail",
          label: r("report.common.emailSchedule"),
          onClick: () => {
            _.value = !0;
          }
        },
        // {
        //   icon: "article",
        //   label: "Set as Default",
        //   onClick: async () => {
        //     await promptLoader(async () => {
        //       if (currentReport.value) {
        //         try {
        //           await reportStore.updateDefaultReport(currentReport.value.id)
        //           snackbar.open({
        //             message: `Set ${currentReport.value.name} as default successful`,
        //             type: "success"
        //           })
        //         } catch (err) {
        //           snackbar.open({ message: JSON.stringify(err), type: "error" })
        //         }
        //       }
        //     })
        //   }
        // },
        {
          icon: "content_copy",
          label: r("report.common.copyID"),
          onClick: () => {
            t.value && (navigator.clipboard.writeText(c.value), i.open({ message: r("report.messages.copiedReportIdToClipboard"), type: "success" }));
          }
        },
        {
          icon: "report_jobs",
          label: r("report.common.reportStatus"),
          onClick: () => {
            k();
          }
        }
      ];
      return B.value || l.splice(1, 0, {
        icon: "download",
        label: r("report.common.accountingExport"),
        onClick: C
      }), l;
    });
    async function k() {
      b.isOpenJobDialog = !0, await b.getJobs();
    }
    function T() {
      N.value = JSON.parse(JSON.stringify(t.value)), m.push({
        name: "reportEditor",
        query: d.query
      });
    }
    async function j() {
      if (!q.value) {
        let e = `Your current plan only support up to ${y.value} custom reports.`;
        y.value === 0 && (e = "Your current plan does not support custom report."), i.open({
          message: e,
          type: "error"
        });
        return;
      }
      if (!t.value) {
        i.open({ message: "No Widget", type: "error" });
        return;
      }
      if (await E({
        title: `${r("report.messages.saveReport")}`,
        message: r("report.messages.confirmAction", {
          action: r("report.common.duplicate"),
          name: t.value.name
        })
      }) && t.value) {
        const e = JSON.parse(JSON.stringify(t.value));
        e.editable = !0, e.id = "", e.name = `${e.name} duplicate`;
        try {
          await I(async () => {
            await f.createOrUpdateReport(e), f.updateCurrentReportId(c.value), m.push({
              name: "reportView",
              query: d.query
            });
          });
        } catch (s) {
          console.log(s), i.open({ message: "Failed to clone report", type: "error" });
        }
      }
    }
    function C() {
      A.value = !0;
    }
    function F() {
      t.value && (navigator.clipboard.writeText(c.value), i.open({ message: "Copied report ID to clipboard", type: "success" }));
    }
    const x = v(() => t.value ? t.value.tiles : []), V = v(() => {
      var e;
      let l = [
        (e = t == null ? void 0 : t.value) != null && e.editable ? { label: r("report.common.edit"), value: "edit", isPrimary: !1 } : {
          label: r("report.common.duplicate"),
          value: "duplicate",
          isPrimary: !1
        },
        {
          label: r("report.common.download"),
          value: "download",
          isPrimary: !0,
          prependIcon: "download"
        },
        { label: r("report.schedule.sendEmail"), value: "email", isPrimary: !0 }
      ];
      return n.value.xs && (l = [
        ...l,
        { label: "Send Email", value: "email", isPrimary: !1 },
        { label: "Job Delivery Status", value: "report_jobs", isPrimary: !1 },
        { label: "Email schedule", value: "schedule_email", isPrimary: !1 },
        { label: "Accounting export", value: "export", isPrimary: !1 },
        { label: "Copy ID", value: "content_copy", isPrimary: !1 }
      ], l = l.filter((s) => s.value !== "edit")), l;
    });
    return (l, e) => {
      const s = H("FmPageHead");
      return u(), g(Z, null, {
        default: M(() => {
          var S;
          return [
            h(s, {
              title: o(pe)(o(r), o(P), (S = o(t)) == null ? void 0 : S.name, "other"),
              "back-button": !0,
              actions: V.value,
              "onClick:back": e[0] || (e[0] = (a) => o(m).push({ name: "reports", query: o(d).query })),
              "onClick:action": e[1] || (e[1] = (a) => {
                a === "duplicate" && j(), a === "download" && (O.value = !0), a === "email" && (J.value = !0), a === "edit" && T(), a === "schedule_email" && (_.value = !0), a === "content_copy" && F(), a === "export" && C(), a === "report_jobs" && k();
              })
            }, null, 8, ["title", "actions"]),
            D("div", {
              class: R(`flex flex-col overflow-y-auto ${o(n).xs || o(n).sm ? "pb-20" : ""}`),
              style: Y({ height: `calc(100% - ${o(n).lg || o(n).md ? 72 : 64}px)` })
            }, [
              D("div", me, [
                h(U, {
                  "model-value": o(n).lg || o(n).md ? L.value : []
                }, null, 8, ["model-value"])
              ]),
              o(t) && x.value.length > 0 ? (u(), $("div", {
                key: 0,
                class: R(["grid grid-cols-12", o(n).sm || o(n).xs ? "px-3 gap-[16px]" : "px-24 gap-[24px]"])
              }, [
                (u(!0), $(G, null, Q(x.value, (a, w) => (u(), g(W, {
                  key: `widget_${o(c)}_${w}`,
                  "model-value": a,
                  index: w
                }, null, 8, ["model-value", "index"]))), 128))
              ], 2)) : (u(), g(ae, { key: 1 }))
            ], 6)
          ];
        }),
        _: 1
      });
    };
  }
});
export {
  Re as default
};
