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