import { HEADING_MARK, LINE_END } from "#configuration/constants/inventory.constants"; import type { LessonSeed, ProfilePrinciple } from "#types/lesson.types"; import { PROFILE_PRINCIPLE_MARK, PROFILE_SECTION_HEADING, SEED_ID_SEPARATOR, SEED_LEVELS, } from "#configuration/constants/seed.constants"; import { slugify } from "@govlab/context/algo"; const SOURCE = "profile"; const SECTION_MARK = `${HEADING_MARK} `; const sectionBody = function sectionBody(text: string): string | null { const start = text.indexOf(PROFILE_SECTION_HEADING); if (start === -1) { return null; } const afterHeading = start + PROFILE_SECTION_HEADING.length; const next = text.indexOf(LINE_END + SECTION_MARK, afterHeading); return text.slice(afterHeading, next === -1 ? text.length : next); }; export const profilePrinciples = function profilePrinciples(text: string): ProfilePrinciple[] { const body = sectionBody(text); if (body === null) { return []; } const collected: { heading: string; lines: string[] }[] = []; for (const line of body.split(LINE_END)) { if (line.startsWith(PROFILE_PRINCIPLE_MARK)) { collected.push({ heading: line.slice(PROFILE_PRINCIPLE_MARK.length).trim(), lines: [] }); continue; } const last = collected.at(-1); if (last !== undefined && line.trim().length > 0) { last.lines.push(line.trim()); } } return collected.map((entry) => ({ body: entry.lines.join(" "), heading: entry.heading })); }; export const profileSeeds = function profileSeeds(text: string): LessonSeed[] { return profilePrinciples(text).map((principle) => ({ application: [], boundary: null, cause: [], decision: principle.heading, domain: SOURCE, failureMode: null, id: SOURCE + SEED_ID_SEPARATOR + slugify(principle.heading), level: SEED_LEVELS[SOURCE], principle: principle.body, problem: null, source: SOURCE, validation: null, })); };