import { defineComponent as N, computed as y, h as p, withModifiers as U, ref as V, resolveComponent as _, createBlock as k, openBlock as i, unref as d, withCtx as h, createElementBlock as D, createVNode as f, Fragment as H, createElementVNode as s, renderList as T, toDisplayString as S, createCommentVNode as I } from "vue";
import { A as c, Q as W, u as Q, G, a5 as J } from "./index.vue_vue_type_script_setup_true_lang-BMVa5UfU.js";
import { useI18n as X } from "@feedmepos/mf-common";
import { components as Z, useDialog as ee, useSnackbar as te } from "@feedmepos/ui-library";
import { _ as ne } from "./ResponsiveFabButton.vue_vue_type_script_setup_true_lang-Cn-3MRBO.js";
import { _ as ae } from "./app-D-J2cjak.js";
import { d as oe, _ as le, u as se, c as re } from "./scheduler-D3ecaFZK.js";
import { _ as ue } from "./PageLayout.vue_vue_type_script_setup_true_lang-2f0QfweQ.js";
const ce = {
  class: "flex flex-1 justify-end"
}, ie = {
  class: "flex justify-end items-center"
}, de = {
  class: "w-[320px] xs:w-full"
}, me = {
  class: "grow space-y-4"
}, pe = {
  class: "fm-typo-en-body-lg-600"
}, fe = {
  class: "fm-typo-en-body-md-400 text-fm-color-typo-secondary"
}, ye = {
  class: "fm-typo-en-body-md-400 text-fm-color-typo-secondary"
}, _e = {
  key: 0
}, De = /* @__PURE__ */ N({
  __name: "Scheduler",
  setup(he) {
    const {
      t: a
    } = X(), M = y(() => [{
      accessorKey: "name",
      header: () => a("menu.scheduler.table.columns.name"),
      cell: (e) => e.getValue(),
      enableSorting: !0
    }, {
      accessorKey: "repeat",
      header: () => a("menu.scheduler.table.columns.repeat"),
      cell: (e) => {
        const {
          repeat: t,
          weekDay: l
        } = e.row.original;
        return F(t, l);
      },
      enableSorting: !1
    }, {
      accessorKey: "timePeriod",
      header: () => a("menu.scheduler.table.columns.period"),
      cell: (e) => {
        const t = e.getValue();
        return p("div", {
          class: ""
        }, t && t.length > 0 ? t.map((l) => p("div", `${l.start} - ${l.end}`)) : a("menu.common.labels.all_day"));
      },
      enableSorting: !1
    }, {
      accessorKey: "items",
      header: () => a("menu.scheduler.table.columns.products"),
      cell: (e) => {
        const t = e.getValue();
        return a("menu.scheduler.table.products_linked", {
          count: t.length
        });
      },
      enableSorting: !1
    }, {
      accessorKey: "_id",
      header: "",
      size: 52,
      enableSorting: !1,
      cell: (e) => p("div", {
        class: "flex justify-end"
      }, [p(Z.FmIcon, {
        name: "delete",
        outline: !0,
        color: "neutral-gray-400",
        onClick: U(() => {
          A(e.row.original);
        }, ["stop", "prevent"])
      })])
    }]), r = {
      [c.F_SCHEDULE_DAY.enum.monday]: "Monday",
      [c.F_SCHEDULE_DAY.enum.tuesday]: "Tuesday",
      [c.F_SCHEDULE_DAY.enum.wednesday]: "Wednesday",
      [c.F_SCHEDULE_DAY.enum.thursday]: "Thursday",
      [c.F_SCHEDULE_DAY.enum.friday]: "Friday",
      [c.F_SCHEDULE_DAY.enum.saturday]: "Saturday",
      [c.F_SCHEDULE_DAY.enum.sunday]: "Sunday"
    }, Y = Object.keys(r).slice(0, 5), z = Object.keys(r).slice(5);
    function F(e, t) {
      if (e === c.F_SCHEDULE_FREQUENCY.enum.daily || t.length === Object.keys(r).length)
        return "Daily";
      const l = Object.keys(r), {
        includes: o,
        excludes: v
      } = l.reduce((u, n) => (t.includes(n) ? u.includes.push(n) : u.excludes.push(n), u), {
        includes: [],
        excludes: []
      });
      return v.length === 1 ? `Daily except ${r[v[0]]}` : E(o, Y) ? "Weekdays" : E(o, z) ? "Weekends" : o.every((u, n) => {
        if (n === 0) return !0;
        const b = l.findIndex((P) => P === u);
        return o[n - 1] === l[b - 1];
      }) ? o.length === 1 ? r[o[0]] : `${r[o[0]]} to ${r[o[o.length - 1]]}` : o.map((u) => r[u]).join(", ");
    }
    function E(e, t) {
      return e.length !== t.length ? !1 : e.every((l, o) => l === t[o]);
    }
    const x = V();
    function L() {
      var e;
      C.value || (e = x.value) == null || e.show();
    }
    function $(e) {
      var t;
      C.value || (t = x.value) == null || t.show(e);
    }
    const q = ee();
    function A(e) {
      let t = [p("span", a("menu.scheduler.dialogs.remove.message", {
        name: e.name
      }))];
      q.open({
        title: a("menu.scheduler.dialogs.remove.title"),
        contentComponent: p("div", t),
        primaryActions: {
          text: a("menu.common.actions.remove"),
          close: !0,
          variant: "destructive"
        },
        secondaryActions: {
          text: a("menu.common.actions.cancel"),
          close: !0
        }
      }).onPrimary(async () => {
        await w.updateMenu(oe(e), e.name, !0);
      });
    }
    async function O(e) {
      const t = e._id ? se(e) : re(e);
      await w.updateMenu(t, e.name);
    }
    const j = Q(), B = y(() => j.categorizedRawMenuItems), m = y(() => j.state.menu.modules.scheduler), w = W(G(te())), C = y(() => w.checkMenuIsLoading()), g = V(""), K = y(() => {
      const e = g.value.toLowerCase();
      return e ? m.value.filter((t) => t.name.toLowerCase().includes(e)) : m.value;
    });
    return (e, t) => {
      const l = _("FmSearch"), o = _("FmTable"), v = _("FmButton"), R = _("FmListItem"), u = _("FmList");
      return i(), k(ue, {
        heading: d(a)("menu.scheduler.title")
      }, {
        "append-heading": h(() => [s("div", ce, [m.value.length > 0 ? (i(), k(ne, {
          key: 0,
          disabled: C.value,
          icon: "add",
          label: d(a)("menu.scheduler.empty.action"),
          variant: "primary",
          onClick: L
        }, null, 8, ["disabled", "label"])) : I("", !0)])]),
        default: h(() => [m.value.length === 0 ? (i(), k(d(J), {
          key: 0,
          action: {
            text: d(a)("menu.scheduler.empty.action"),
            callback: L
          },
          subtitle: d(a)("menu.scheduler.empty.subtitle"),
          title: d(a)("menu.scheduler.empty.title")
        }, null, 8, ["action", "subtitle", "title"])) : (i(), D(H, {
          key: 1
        }, [s("div", ie, [s("div", de, [f(l, {
          modelValue: g.value,
          "onUpdate:modelValue": t[0] || (t[0] = (n) => g.value = n),
          autofocus: "",
          class: "mb-8 xs:px-16 xs:mt-8",
          placeholder: d(a)("menu.scheduler.table.search.placeholder")
        }, null, 8, ["modelValue", "placeholder"])])]), f(ae, {
          class: "xs:hidden block"
        }, {
          default: h(() => [f(o, {
            "column-defs": M.value,
            "page-size": m.value.length,
            "row-data": m.value,
            "search-value": g.value,
            shrinkAt: !1,
            "hide-footer": "",
            "pin-header-row": "",
            onRowClick: t[1] || (t[1] = (n) => $(n.original))
          }, null, 8, ["column-defs", "page-size", "row-data", "search-value"])]),
          _: 1
        }), f(u, {
          class: "flex-col flex-1 min-h-0 overflow-auto xs:flex hidden w-full px-16 pb-64",
          separator: ""
        }, {
          default: h(() => [(i(!0), D(H, null, T(K.value, (n) => (i(), k(R, {
            key: n._id,
            label: n.name,
            style: {
              padding: "12px 0 !important"
            },
            onClick: () => $(n)
          }, {
            default: h(() => [s("div", me, [s("div", pe, S(n.name), 1), s("div", null, [s("div", fe, S(Object.keys(n.items).length) + " product(s) linked ", 1), s("div", ye, [s("div", null, " Repeat: " + S(F(n.repeat, n.weekDay)), 1), n.timePeriod.length > 0 ? (i(), D("span", _e, "Period: ")) : I("", !0), s("span", null, S(n.timePeriod.map((b) => `${b.start} - ${b.end}`).join(", ")), 1)])])]), f(v, {
              icon: "delete",
              variant: "tertiary",
              "text-color": "neutral-gray-400",
              onClick: U(() => A(n), ["stop"])
            }, null, 8, ["onClick"])]),
            _: 2
          }, 1032, ["label", "onClick"]))), 128))]),
          _: 1
        })], 64)), f(le, {
          ref_key: "schedulerDialogRef",
          ref: x,
          "categorized-items": B.value,
          onOnComplete: O
        }, null, 8, ["categorized-items"])]),
        _: 1
      }, 8, ["heading"]);
    };
  }
});
export {
  De as default
};
