import { defineComponent as M, watch as _, ref as E, resolveComponent as f, createBlock as x, openBlock as A, unref as c, withCtx as C, createVNode as V } from "vue";
import { M as l, A as d, u as F } from "./index.vue_vue_type_script_setup_true_lang-BMVa5UfU.js";
import { useI18n as b } from "@feedmepos/mf-common";
import { useDialogChild as P } from "@feedmepos/ui-library";
import { u as v } from "./menuV2-Da02vc32.js";
const y = /* @__PURE__ */ M({
  __name: "AddSubcategoryDialogContentComponent",
  props: {
    initialValue: {}
  },
  setup(o) {
    const r = o, { t: n } = b();
    _(
      () => r.initialValue,
      (i) => {
        t.value = i ?? "";
      }
    );
    const t = E(r.initialValue), { emitData: u, emitAction: p } = P();
    return _(
      t,
      (i) => {
        u(i);
      },
      {
        immediate: !0
      }
    ), (i, s) => {
      const g = f("fm-text-field"), a = f("FmForm");
      return A(), x(a, {
        onValidationSuccess: s[1] || (s[1] = (e) => c(p)("primary"))
      }, {
        default: C(() => [
          V(g, {
            modelValue: t.value,
            "onUpdate:modelValue": s[0] || (s[0] = (e) => t.value = e),
            rules: [(e) => !!e || c(n)("menu.subcategory.dialog.add.name.required")],
            autofocus: "",
            label: c(n)("menu.subcategory.dialog.add.name.label"),
            placeholder: c(n)("menu.subcategory.dialog.add.name.placeholder")
          }, null, 8, ["modelValue", "rules", "label", "placeholder"])
        ]),
        _: 1
      });
    };
  }
}), Y = ({
  dialog: o,
  menuLoader: r,
  isLocalOnly: n = !1
}) => {
  const { t } = b(), u = v().updateMenu;
  function p() {
    const a = E(!1);
    o.open({
      title: t("menu.subcategory.manager.dialog.add.title"),
      contentComponent: y,
      primaryActions: {
        text: t("menu.subcategory.manager.dialog.add.action"),
        close: !1,
        isLoading: a
      },
      secondaryActions: {
        text: t("menu.common.actions.cancel"),
        close: !0
      }
    }).onPrimary(async (e) => {
      if (e)
        try {
          a.value = !0;
          const m = {
            module: d.F_MENU_MODULE_V4_KEY.enum.subCategory,
            operation: l.F_OPERATION_TYPE.create,
            data: {
              name: e,
              position: F().subcategories.length
            }
          };
          n ? await u(m) : await r.updateMenu(m, e), o.close();
        } finally {
          a.value = !1;
        }
    });
  }
  function i(a) {
    o.open({
      title: t("menu.subcategory.manager.dialog.update.title"),
      contentComponent: y,
      contentComponentProps: {
        initialValue: a.name
      },
      primaryActions: {
        text: t("menu.subcategory.manager.dialog.update.action"),
        close: !1
      },
      secondaryActions: {
        text: t("menu.common.actions.cancel"),
        close: !0
      }
    }).onPrimary(async (e) => {
      if (!e) return;
      const m = {
        module: d.F_MENU_MODULE_V4_KEY.enum.subCategory,
        operation: l.F_OPERATION_TYPE.update,
        data: { ...a, name: e ?? a.name }
      };
      n ? await u(m) : await r.updateMenu(m, e), o.close();
    });
  }
  function s(a) {
    o.open({
      title: t("menu.common.dialog.remove.subcategory.title"),
      message: t("menu.common.dialog.remove.subcategory.message", { name: a.name }),
      primaryActions: {
        text: t("menu.common.actions.remove"),
        close: !1
      },
      secondaryActions: {
        text: t("menu.common.actions.cancel"),
        close: !0
      }
    }).onPrimary(async () => {
      const e = {
        module: d.F_MENU_MODULE_V4_KEY.enum.subCategory,
        operation: l.F_OPERATION_TYPE.delete,
        data: a._id
      };
      n ? await u(e) : await r.updateMenu(e, a.name, !0), o.close();
    });
  }
  async function g(a) {
    const e = {
      module: d.F_MENU_MODULE_V4_KEY.enum.subCategory,
      operation: l.F_OPERATION_TYPE.rearrange,
      data: a
    };
    n ? await u(e) : await r.updateMenu(e, "Subcategories");
  }
  return {
    create: p,
    update: i,
    deleteSubcategory: s,
    rearrange: g
  };
};
export {
  Y as u
};
