import { jsx as $ } from "@emotion/react/jsx-runtime";
import { useNetwork as h, useFlagsStore as M, useModuleDictionary as b } from "@m4l/core";
import { useState as m, useEffect as u } from "react";
import { useModal as k } from "../useModal/index.js";
import { WindowConfirm as C } from "../../components/WindowConfirm/WindowConfirm.js";
function x(c) {
  const {
    initialValues: _,
    objectId: t,
    endPoint: d,
    formatDataEnpoint: i,
    queryParamas: s,
    isRemote: p = !0,
    omitCallEnpoint: v = !1
  } = c, { openModal: g, closeModal: w } = k(), [y, n] = m(_), [e, o] = m(
    t === void 0 ? "new" : "edit"
  ), { networkOperation: E } = h(), a = M((r) => r.flagsActions.addFlag), { getLabel: l } = b(), F = () => {
    e === "ready" && o("edit");
  };
  return u(() => {
    let r = !0;
    if (e === "edit") {
      if (v) {
        a("form_loaded"), o("ready");
        return;
      }
      E({
        method: "GET",
        endPoint: s ? `${d}/${t}?${s}` : `${d}/${t}`,
        isRemote: p
      }).then(
        (f) => {
          r && (n(i ? i(f) : f.data), o("reload_values_provider"));
        },
        () => {
          o("error"), g({
            window: /* @__PURE__ */ $(
              C,
              {
                variant: "error",
                title: l("form_add_edit.label_error_title"),
                msg: l("form_add_edit.error_authorization_message"),
                onClickIntro: () => w()
              }
            ),
            variant: "delete"
          }), a("form_loaded");
        }
      );
    } else (e === "new" || e === "reload_values_provider") && (a("form_loaded"), o("ready"));
    return () => {
      r = !1;
    };
  }, [e]), u(() => {
    e === "ready" && t !== void 0 && o("edit");
  }, [t]), { formValues: y, statusLoad: e, setRefresh: F };
}
export {
  x as useFormAddEdit
};
