/** * @file Regroupe les helpers purs de sections, items et déplacement de checklist. */ export function isSectionComplete(section, context) { return section.items.length > 0 && section.items.every((item) => context.clampQty(item.qtyCurrent, item.qtyTarget) >= item.qtyTarget); } export function getSectionHideWhenComplete(section, globalHide) { return typeof section.hideWhenComplete === "boolean" ? section.hideWhenComplete : globalHide; } export function keepChecklistSection(section) { return section.items.length; } export function getChecklistReorderItems(sections) { return sections.flatMap((section) => section.items.map((item) => ({ ...item, sectionId: section.id }))); } export function appendChecklistItems(sections, title, items, context) { const cleanTitle = title.trim(); const existingIndex = sections.findIndex((section) => section.title.toLowerCase() === cleanTitle.toLowerCase()); if (existingIndex >= 0) { return sections.map((section, index) => index === existingIndex ? { ...section, items: [...section.items, ...items] } : section); } return [...sections, { id: context.uid("section"), title: cleanTitle, items }]; } export function findChecklistSectionByItemId(sections, itemId) { return sections.find((section) => section.items.some((item) => item.id === itemId)); } export function renameChecklistSection(sections, sectionId, title) { const cleanTitle = title.trim(); const section = sections.find((entry) => entry.id === sectionId); if (!section) return sections; const existingSection = sections.find((entry) => entry.id !== sectionId && entry.title.toLowerCase() === cleanTitle.toLowerCase()); if (!existingSection) { return sections.map((entry) => entry.id === sectionId ? { ...entry, title: cleanTitle } : entry).filter(keepChecklistSection); } return sections.map((entry) => { if (entry.id === existingSection.id) return { ...entry, items: [...entry.items, ...section.items] }; return entry; }).filter((entry) => entry.id !== sectionId).filter(keepChecklistSection); } export function updateChecklistItem(sections, itemId, updater) { return sections.map((section) => ({ ...section, items: section.items.map((item) => item.id === itemId ? updater(item) : item).filter((item) => item.label) })).filter(keepChecklistSection); } export function moveChecklistItemToCategory(sections, itemId, title, context) { const cleanTitle = title.trim(); const sourceSection = findChecklistSectionByItemId(sections, itemId); const item = sourceSection?.items.find((entry) => entry.id === itemId); if (!sourceSection || !item || sourceSection.title.toLowerCase() === cleanTitle.toLowerCase()) return sections; const targetSection = sections.find((section) => section.title.toLowerCase() === cleanTitle.toLowerCase()); const sectionsWithoutItem = sections.map((section) => ({ ...section, items: section.items.filter((entry) => entry.id !== itemId) })).filter(keepChecklistSection); if (targetSection) { return sectionsWithoutItem.map((section) => section.id === targetSection.id ? { ...section, items: [...section.items, item] } : section); } return [...sectionsWithoutItem, { id: context.uid("section"), title: cleanTitle, items: [item] }]; } export function moveChecklistItem(sections, operation, context) { const sourceSection = findChecklistSectionByItemId(sections, operation.sourceId); const sourceItem = sourceSection?.items.find((item) => item.id === operation.sourceId); if (!sourceSection || !sourceItem) return sections; if (operation.targetType === "boundary") return moveChecklistItemToUngroupedBoundary(sections, sourceSection, sourceItem, operation, context); const targetSection = operation.targetType === "item" ? findChecklistSectionByItemId(sections, operation.targetId) : sections.find((section) => section.id === operation.targetGroup); if (!targetSection) return sections; const sectionsWithoutItem = sections.map((section) => ({ ...section, items: section.items.filter((item) => item.id !== operation.sourceId) })).filter(keepChecklistSection); return sectionsWithoutItem.map((section) => { if (section.id !== targetSection.id) return section; if (operation.targetType === "item") { const targetIndex = section.items.findIndex((item) => item.id === operation.targetId); if (targetIndex < 0) return { ...section, items: [...section.items, sourceItem] }; const nextItems = [...section.items]; nextItems.splice(operation.placement === "after" ? targetIndex + 1 : targetIndex, 0, sourceItem); return { ...section, items: nextItems }; } return { ...section, items: operation.placement === "after" ? [...section.items, sourceItem] : [sourceItem, ...section.items] }; }); } function moveChecklistItemToUngroupedBoundary(sections, sourceSection, sourceItem, operation, context) { const targetIndex = sections.findIndex((section) => section.id === operation.targetGroup); if (targetIndex < 0) return sections; const preferredUngroupedSection = sourceSection.title ? sections.find((section) => !section.title && section.id !== sourceSection.id) : sourceSection; const ungroupedSectionId = preferredUngroupedSection?.id || context.uid("section"); const baseSections = sections .map((section) => ({ ...section, items: section.items.filter((item) => item.id !== sourceItem.id) })) .filter((section) => section.id !== ungroupedSectionId) .filter(keepChecklistSection); const targetSection = sections[targetIndex]; const nextTargetIndex = baseSections.findIndex((section) => section.id === targetSection.id); const insertIndex = nextTargetIndex < 0 ? Math.min(targetIndex, baseSections.length) : nextTargetIndex + (operation.placement === "after" ? 1 : 0); const ungroupedSection = { id: ungroupedSectionId, title: "", items: [...(preferredUngroupedSection?.items || []).filter((item) => item.id !== sourceItem.id), sourceItem] }; const nextSections = [...baseSections]; nextSections.splice(insertIndex, 0, ungroupedSection); return nextSections; }