"use client";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
import { format } from "date-fns/format";
import { parseISO } from "date-fns/parseISO";
import { Input } from "@open-mercato/ui/primitives/input";
import { Textarea } from "@open-mercato/ui/primitives/textarea";
import { DatePicker } from "@open-mercato/ui/primitives/date-picker";
import { DictionarySelectField } from "../../formConfig.js";
import { DealFormField } from "./DealFormField.js";
import { PipelineSelect } from "./PipelineSelect.js";
import { PipelineStageSelect } from "./PipelineStageSelect.js";
import { SuffixInput } from "./SuffixInput.js";
import { DealCurrencyField } from "./DealCurrencyField.js";
import { sanitizeAmount, sanitizeProbability } from "./dealNumericInput.js";
function toDate(value) {
  if (!value) return null;
  const parsed = parseISO(value);
  return Number.isNaN(parsed.getTime()) ? null : parsed;
}
function DealDetailsFields({
  values,
  errors,
  isSubmitting,
  patch,
  onPipelineChange,
  pipelines,
  stages,
  statusLabels,
  tr
}) {
  return /* @__PURE__ */ jsxs(Fragment, { children: [
    /* @__PURE__ */ jsx(
      DealFormField,
      {
        fieldId: "title",
        label: tr("customers.deals.create.fields.title", "Deal title"),
        required: true,
        hint: tr("customers.deals.create.hints.title", "Short, descriptive name shown on pipeline cards"),
        error: errors.title,
        children: /* @__PURE__ */ jsx(
          Input,
          {
            value: values.title,
            onChange: (event) => patch({ title: event.target.value }),
            "aria-invalid": errors.title ? true : void 0,
            disabled: isSubmitting
          }
        )
      }
    ),
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-4 md:grid-cols-2", children: [
      /* @__PURE__ */ jsx(DealFormField, { fieldId: "status", label: tr("customers.people.detail.deals.fields.status", "Status"), children: /* @__PURE__ */ jsx(
        DictionarySelectField,
        {
          kind: "deal-statuses",
          value: values.status || void 0,
          onChange: (next) => patch({ status: next ?? "" }),
          labels: statusLabels,
          selectClassName: "w-full",
          showActiveAppearance: false
        }
      ) }),
      /* @__PURE__ */ jsx(DealFormField, { fieldId: "pipelineId", label: tr("customers.people.detail.deals.fields.pipeline", "Pipeline"), children: /* @__PURE__ */ jsx(
        PipelineSelect,
        {
          pipelines,
          value: values.pipelineId,
          onChange: onPipelineChange,
          disabled: isSubmitting,
          placeholder: tr("customers.deals.form.pipeline.placeholder", "Select pipeline\u2026")
        }
      ) })
    ] }),
    /* @__PURE__ */ jsx(
      DealFormField,
      {
        fieldId: "pipelineStageId",
        label: tr("customers.people.detail.deals.fields.pipelineStage", "Pipeline stage"),
        hint: tr("customers.deals.create.hints.pipelineStage", "Stages depend on the selected pipeline"),
        children: /* @__PURE__ */ jsx(
          PipelineStageSelect,
          {
            stages,
            value: values.pipelineStageId,
            onChange: (id) => patch({ pipelineStageId: id }),
            disabled: isSubmitting || !values.pipelineId,
            placeholder: tr("customers.deals.form.pipelineStage.placeholder", "Select stage\u2026"),
            formatCount: (position, total) => tr("customers.deals.create.fields.stageOf", "\xB7 stage {position} of {total}", { position, total })
          }
        )
      }
    ),
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-4 md:grid-cols-2", children: [
      /* @__PURE__ */ jsx(
        DealFormField,
        {
          fieldId: "valueAmount",
          label: tr("customers.deals.create.fields.valueAmount", "Deal value"),
          hint: tr("customers.deals.create.hints.valueAmount", "Potential revenue from this opportunity"),
          error: errors.valueAmount,
          children: /* @__PURE__ */ jsx(
            SuffixInput,
            {
              suffix: values.valueCurrency,
              inputMode: "decimal",
              value: values.valueAmount,
              onChange: (event) => patch({ valueAmount: sanitizeAmount(event.target.value) }),
              placeholder: "0",
              "aria-invalid": errors.valueAmount ? true : void 0,
              disabled: isSubmitting
            }
          )
        }
      ),
      /* @__PURE__ */ jsx(DealFormField, { fieldId: "valueCurrency", label: tr("customers.people.detail.deals.fields.valueCurrency", "Currency"), children: /* @__PURE__ */ jsx(
        DealCurrencyField,
        {
          value: values.valueCurrency,
          onChange: (code) => patch({ valueCurrency: code }),
          disabled: isSubmitting
        }
      ) })
    ] }),
    /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-1 gap-4 md:grid-cols-2", children: [
      /* @__PURE__ */ jsx(
        DealFormField,
        {
          fieldId: "probability",
          label: tr("customers.deals.create.fields.probability", "Probability"),
          hint: tr("customers.deals.create.hints.probability", "0 \u2013 100%, used for weighted pipeline value"),
          error: errors.probability,
          children: /* @__PURE__ */ jsx(
            SuffixInput,
            {
              suffix: "%",
              inputMode: "numeric",
              value: values.probability,
              onChange: (event) => patch({ probability: sanitizeProbability(event.target.value) }),
              placeholder: "0",
              "aria-invalid": errors.probability ? true : void 0,
              disabled: isSubmitting
            }
          )
        }
      ),
      /* @__PURE__ */ jsx(DealFormField, { fieldId: "expectedCloseAt", label: tr("customers.deals.create.fields.expectedCloseAt", "Expected close date"), children: /* @__PURE__ */ jsx(
        DatePicker,
        {
          value: toDate(values.expectedCloseAt),
          onChange: (date) => patch({ expectedCloseAt: date ? format(date, "yyyy-MM-dd") : "" }),
          disabled: isSubmitting,
          placeholder: tr("customers.deals.create.fields.datePlaceholder", "Pick a date")
        }
      ) })
    ] }),
    /* @__PURE__ */ jsx(DealFormField, { fieldId: "description", label: tr("customers.people.detail.deals.fields.description", "Description"), children: /* @__PURE__ */ jsx(
      Textarea,
      {
        value: values.description,
        onChange: (event) => patch({ description: event.target.value }),
        disabled: isSubmitting
      }
    ) })
  ] });
}
var DealDetailsFields_default = DealDetailsFields;
export {
  DealDetailsFields,
  DealDetailsFields_default as default
};
//# sourceMappingURL=DealDetailsFields.js.map
