"use client";
import { jsx } from "react/jsx-runtime";
import { Page, PageBody } from "@open-mercato/ui/backend/Page";
import { CrudForm } from "@open-mercato/ui/backend/CrudForm";
import { ErrorMessage, LoadingMessage } from "@open-mercato/ui/backend/detail";
import { E } from "../../../../../../../generated/entities.ids.generated.js";
import { useT } from "@open-mercato/shared/lib/i18n/context";
import * as React from "react";
import { updateCrud } from "@open-mercato/ui/backend/utils/crud";
import { useFeatureToggleItem } from "@open-mercato/core/modules/feature_toggles/components/hooks/useFeatureToggleItem";
import { createFieldDefinitions, createFormGroups } from "@open-mercato/core/modules/feature_toggles/components/formConfig";
function EditFeatureTogglePage({ params }) {
  const { id } = params ?? {};
  const t = useT();
  const fields = createFieldDefinitions(t);
  const formGroups = createFormGroups(t);
  const { data: featureToggleItem, isLoading, isError } = useFeatureToggleItem(id);
  const initialValues = React.useMemo(() => {
    if (!featureToggleItem) return null;
    return {
      id: featureToggleItem.id ?? (id ? String(id) : ""),
      identifier: featureToggleItem.identifier,
      name: featureToggleItem.name,
      description: featureToggleItem.description,
      category: featureToggleItem.category,
      type: featureToggleItem.type,
      defaultValue: featureToggleItem.defaultValue
    };
  }, [featureToggleItem, id]);
  if (!initialValues) {
    return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: isError && !isLoading ? /* @__PURE__ */ jsx(ErrorMessage, { label: t("feature_toggles.form.errors.load", "Failed to load feature toggle") }) : /* @__PURE__ */ jsx(LoadingMessage, { label: t("feature_toggles.form.loading", "Loading feature toggles") }) }) });
  }
  return /* @__PURE__ */ jsx(Page, { children: /* @__PURE__ */ jsx(PageBody, { children: /* @__PURE__ */ jsx(
    CrudForm,
    {
      title: t("feature_toggles.form.title.edit", "Edit Feature Toggle"),
      backHref: "/backend/feature-toggles/global",
      versionHistory: { resourceKind: "feature_toggles.global", resourceId: id ? String(id) : "" },
      fields,
      entityId: E.feature_toggles.feature_toggle,
      initialValues,
      optimisticLockUpdatedAt: featureToggleItem?.updatedAt ?? null,
      isLoading,
      groups: formGroups,
      loadingMessage: t("feature_toggles.form.loading", "Loading feature toggles"),
      submitLabel: t("feature_toggles.form.action.save", "Save"),
      cancelHref: "/backend/feature-toggles/global",
      successRedirect: `/backend/feature-toggles/global`,
      onSubmit: async (values) => {
        if (!id) return;
        const payload = {
          id: id ? String(id) : "",
          identifier: values.identifier,
          name: values.name,
          description: values.description,
          category: values.category,
          type: values.type,
          defaultValue: values.defaultValue
        };
        await updateCrud("feature_toggles/global", payload);
      }
    },
    initialValues ? "ft-edit-loaded" : "ft-edit-loading"
  ) }) });
}
export {
  EditFeatureTogglePage as default
};
//# sourceMappingURL=page.js.map
