import { Button } from '@components/common/ui/Button.js'; import { Switch } from '@components/common/ui/Switch.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { ArrowDown, ArrowUp } from 'lucide-react'; import React from 'react'; import { useFormContext } from 'react-hook-form'; /** * Shared § 6.2 rules editor for the three admin surfaces (settings card, * product override, category override). The whole config lives in local * state and is written into the form as ONE headless registered path via * setValue — the form body then carries a real nested object with real * booleans/numbers (the § 5.3 AJV schemas are strict; a stringified copy or * string leaves would fail the save), and reordering never fights * react-hook-form over index-shifted field names. */ export interface RulesEditorRule { type: 'same_category' | 'same_collection' | 'same_attribute_values'; enabled: boolean; attributeCodes?: string[]; scope?: 'same_category' | 'same_collection'; } export interface RulesEditorConfig { enabled: boolean; rules: RulesEditorRule[]; priceBand?: { enabled: boolean; percent?: number }; fallbackToBestsellers?: boolean; } const RULE_LABELS: Record = { same_category: 'Same category', same_collection: 'Same collection', same_attribute_values: 'Same attribute values' }; const ALL_RULE_TYPES = Object.keys(RULE_LABELS) as RulesEditorRule['type'][]; /** Every type exactly once, stored order first — array order IS priority. */ function normalizeConfig(initial?: RulesEditorConfig | null): RulesEditorConfig { const seen: RulesEditorRule[] = []; for (const rule of initial?.rules || []) { if ( ALL_RULE_TYPES.includes(rule?.type) && !seen.some((existing) => existing.type === rule.type) ) { seen.push({ type: rule.type, enabled: rule.enabled === true, attributeCodes: Array.isArray(rule.attributeCodes) ? rule.attributeCodes.filter((code) => typeof code === 'string') : [], scope: rule.scope === 'same_collection' ? 'same_collection' : 'same_category' }); } } for (const type of ALL_RULE_TYPES) { if (!seen.some((rule) => rule.type === type)) { seen.push({ type, enabled: false, attributeCodes: [], scope: 'same_category' }); } } return { enabled: initial?.enabled !== false, rules: seen, priceBand: { enabled: initial?.priceBand?.enabled === true, percent: typeof initial?.priceBand?.percent === 'number' ? initial.priceBand.percent : 30 }, fallbackToBestsellers: initial?.fallbackToBestsellers !== false }; } /** Strip editor-only normalization before it hits the strict AJV schema. */ function toPayload(config: RulesEditorConfig): RulesEditorConfig { return { enabled: config.enabled, rules: config.rules.map((rule) => rule.type === 'same_attribute_values' ? { type: rule.type, enabled: rule.enabled, attributeCodes: rule.attributeCodes || [], scope: rule.scope || 'same_category' } : { type: rule.type, enabled: rule.enabled } ), priceBand: { enabled: config.priceBand?.enabled === true, percent: config.priceBand?.percent ?? 30 }, fallbackToBestsellers: config.fallbackToBestsellers !== false }; } /** * Controlled Switch — deliberately NOT the core ToggleField: every control in * this editor writes the config through ONE headless setValue path (see the * component doc above); a per-leaf RHF field would double-register state, * shift names on reorder, and post string leaves against the strict § 6.2 * schema. Visual consistency comes from wrapping the same ui/Switch * primitive that ToggleField itself renders. The label text carries its own * onClick because base-ui switches ignore