"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 { useT } from "@open-mercato/shared/lib/i18n/context";
const normalizeCustomFieldSubmitValue = (value) => {
  const normalized = normalizeCustomFieldValues({ value });
  return normalized.value;
};
const buildTeamPayload = (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 customFields = collectCustomFieldValues(values, { transform: normalizeCustomFieldSubmitValue });
  return {
    ...options.id ? { id: options.id } : {},
    name,
    description,
    isActive: values.isActive ?? true,
    ...Object.keys(customFields).length ? { customFields } : {}
  };
};
function TeamForm(props) {
  const {
    title,
    submitLabel,
    backHref,
    cancelHref,
    initialValues,
    onSubmit,
    onDelete,
    isLoading,
    loadingMessage,
    extraActions
  } = props;
  const t = useT();
  const fields = React.useMemo(() => [
    { id: "name", label: t("staff.teams.form.fields.name", "Name"), type: "text", required: true },
    { id: "description", label: t("staff.teams.form.fields.description", "Description"), type: "richtext", editor: "uiw" },
    { id: "isActive", label: t("staff.teams.form.fields.active", "Active"), type: "checkbox" }
  ], [t]);
  const groups = React.useMemo(() => [
    { id: "details", fields: ["name", "description", "isActive"] },
    { id: "custom", title: t("entities.customFields.title", "Custom Attributes"), column: 2, kind: "customFields" }
  ], [t]);
  return /* @__PURE__ */ jsx(
    CrudForm,
    {
      title,
      backHref,
      cancelHref,
      versionHistory: initialValues.id ? { resourceKind: "staff.team", resourceId: String(initialValues.id) } : void 0,
      submitLabel,
      fields,
      groups,
      entityId: E.staff.staff_team,
      initialValues,
      optimisticLockUpdatedAt: initialValues.updatedAt,
      onSubmit,
      onDelete,
      isLoading,
      loadingMessage,
      extraActions
    }
  );
}
export {
  TeamForm,
  buildTeamPayload
};
//# sourceMappingURL=TeamForm.js.map
