"use client";
import { jsx } from "react/jsx-runtime";
import * as React from "react";
import { CrudForm } from "@open-mercato/ui/backend/CrudForm";
import { collectCustomFieldValues } from "@open-mercato/ui/backend/utils/customFieldValues";
import { normalizeCustomFieldValues } from "@open-mercato/shared/lib/custom-fields/normalize";
import { E } from "../../../generated/entities.ids.generated.js";
import { AppearanceSelector } from "@open-mercato/core/modules/dictionaries/components/AppearanceSelector";
import { useT } from "@open-mercato/shared/lib/i18n/context";
const normalizeCustomFieldSubmitValue = (value) => {
  const normalized = normalizeCustomFieldValues({ value });
  return normalized.value;
};
const buildResourceTypePayload = (values, options = {}) => {
  const name = typeof values.name === "string" ? values.name.trim() : "";
  const description = typeof values.description === "string" && values.description.trim().length ? values.description.trim() : null;
  const appearance = values.appearance && typeof values.appearance === "object" ? values.appearance : {};
  const customFields = collectCustomFieldValues(values, { transform: normalizeCustomFieldSubmitValue });
  return {
    ...options.id ? { id: options.id } : {},
    name,
    description,
    appearanceIcon: appearance.icon ?? null,
    appearanceColor: appearance.color ?? null,
    ...Object.keys(customFields).length ? { customFields } : {}
  };
};
function ResourceTypeCrudForm({
  mode,
  initialValues,
  isLoading,
  onSubmit,
  onDelete,
  deleteVisible
}) {
  const t = useT();
  const appearanceLabels = React.useMemo(() => ({
    colorLabel: t("resources.resourceTypes.form.appearance.colorLabel", "Color"),
    colorHelp: t("resources.resourceTypes.form.appearance.colorHelp", "Pick a color for this resource type."),
    colorClearLabel: t("resources.resourceTypes.form.appearance.colorClear", "Clear color"),
    iconLabel: t("resources.resourceTypes.form.appearance.iconLabel", "Icon"),
    iconPlaceholder: t("resources.resourceTypes.form.appearance.iconPlaceholder", "Type an emoji or icon name"),
    iconPickerTriggerLabel: t("resources.resourceTypes.form.appearance.iconPicker", "Browse icons"),
    iconSearchPlaceholder: t("resources.resourceTypes.form.appearance.iconSearch", "Search icons or emojis\u2026"),
    iconSearchEmptyLabel: t("resources.resourceTypes.form.appearance.iconSearchEmpty", "No icons match your search"),
    iconSuggestionsLabel: t("resources.resourceTypes.form.appearance.iconSuggestions", "Suggestions"),
    iconClearLabel: t("resources.resourceTypes.form.appearance.iconClear", "Clear icon"),
    previewEmptyLabel: t("resources.resourceTypes.form.appearance.previewEmpty", "No appearance selected")
  }), [t]);
  const fields = React.useMemo(() => [
    { id: "name", label: t("resources.resourceTypes.form.name", "Name"), type: "text", required: true },
    { id: "description", label: t("resources.resourceTypes.form.description", "Description"), type: "richtext", editor: "uiw" },
    {
      id: "appearance",
      label: t("resources.resourceTypes.form.appearance.label", "Appearance"),
      type: "custom",
      component: ({ value, setValue }) => {
        const current = value && typeof value === "object" ? value : {};
        return /* @__PURE__ */ jsx(
          AppearanceSelector,
          {
            icon: current.icon ?? null,
            color: current.color ?? null,
            onIconChange: (next) => setValue({ ...current, icon: next }),
            onColorChange: (next) => setValue({ ...current, color: next }),
            labels: appearanceLabels
          }
        );
      }
    }
  ], [appearanceLabels, t]);
  const groups = React.useMemo(() => [
    { id: "details", fields: ["name", "description", "appearance"] },
    { id: "custom", title: t("entities.customFields.title", "Custom Attributes"), column: 2, kind: "customFields" }
  ], [t]);
  return /* @__PURE__ */ jsx(
    CrudForm,
    {
      title: mode === "create" ? t("resources.resourceTypes.form.createTitle", "Add resource type") : t("resources.resourceTypes.form.editTitle", "Edit resource type"),
      backHref: "/backend/resources/resource-types",
      versionHistory: mode === "edit" ? { resourceKind: "resources.resourceType", resourceId: initialValues.id ?? "" } : void 0,
      cancelHref: "/backend/resources/resource-types",
      submitLabel: t("resources.resourceTypes.form.save", "Save"),
      fields,
      groups,
      entityId: E.resources.resources_resource_type,
      initialValues,
      optimisticLockUpdatedAt: mode === "edit" ? initialValues.updatedAt : void 0,
      isLoading,
      onSubmit,
      onDelete: mode === "edit" ? onDelete : void 0,
      deleteVisible: typeof deleteVisible === "boolean" ? deleteVisible : mode === "edit"
    }
  );
}
export {
  ResourceTypeCrudForm,
  buildResourceTypePayload
};
//# sourceMappingURL=ResourceTypeCrudForm.js.map
