domain/converters/reason.fragment.converter.ts

domain/converters/reason.fragment.converter.ts is a file in Bane's Lab Site. 167 lines of code and 10 definitions.

import { CONTRACTS_LABEL, INVARIANT_LABEL } from "#configuration/strings/ontology.strings";
import {
    DETECTORS_LABEL,
    DIMENSION_LABEL,
    EVIDENCE_GROUNDS_LABEL,
    EVIDENCE_LABEL,
    FAILURE_MODES_LABEL,
    FIELDS_LABEL,
    FIT_LABEL,
    LENS_INTRO,
    LENS_LABEL,
    LENS_TITLE,
    MATH_INTRO,
    MATH_TITLE,
    NATURE_LABEL,
    OPTIONAL_LABEL,
    PREDICATE_FAMILY_LABEL,
    PREDICATE_GROUNDS_LABEL,
    PREDICATE_TITLE,
    QUESTION_LABEL,
    REQUIRED_LABEL,
    STUDIES_LABEL,
    SURFACES_LABEL,
    SURFACE_INTRO,
    SURFACE_TITLE,
    TECHNIQUES_LABEL,
    TEST_SURFACES_LABEL,
    UNIVERSAL_AXIS_LABEL,
    VERDICTS_LABEL,
    YIELDS_SHAPE_LABEL,
} from "#configuration/strings/reason.strings";
import { FACE_SEPARATOR, REASON_FACE } from "@govlab/constants";
import { LENS_SECTION_ID, MATH_SECTION_ID, REASON_ANCHOR, SURFACE_SECTION_ID } from "#core/ids/ontology.ids";
import type { LensView, MathDomainView, MathTypeView, ReasonView, TestSurfaceView } from "#types/reason.types";
import type { Section, Subsection } from "#types/document.types";
import {
    chips,
    codeOf,
    fields,
    glossary,
    labeled,
    linkList,
    linkOf,
    linkRow,
    linked,
    plainList,
    row,
} from "#domain/converters/ontology.converter";
import type { Block } from "#types/block.types";
import { REASON_SECTION_ICON } from "#configuration/icons/ontology.icons";
import type { ResolutionView } from "#types/ontology.types";
import { TEXT_LANGUAGE } from "#configuration/constants/code.constants";
import { checkBlocks } from "#domain/converters/check.converter";

export const reasonRecord = function reasonRecord(
    anchor: string,
    title: string,
    blocks: readonly Block[],
    resolution: ResolutionView,
): Subsection {
    return {
        blocks: [...blocks, ...checkBlocks(resolution, REASON_FACE + FACE_SEPARATOR + anchor)],
        id: REASON_ANCHOR + anchor,
        title,
    };
};

const mathTypeSubsection = function mathTypeSubsection(mathType: MathTypeView, resolution: ResolutionView): Subsection {
    return reasonRecord(
        mathType.anchor,
        mathType.id,
        [
            glossary([
                row(QUESTION_LABEL, mathType.question),
                ...linkRow(FIELDS_LABEL, mathType.mathDomains),
                row(PREDICATE_FAMILY_LABEL, mathType.predicateFamily),
                row(YIELDS_SHAPE_LABEL, mathType.yieldsShape),
                ...linkRow(CONTRACTS_LABEL, mathType.contracts),
            ]),
        ],
        resolution,
    );
};

const mathDomainSubsection = function mathDomainSubsection(
    domain: MathDomainView,
    resolution: ResolutionView,
): Subsection {
    return reasonRecord(
        domain.anchor,
        domain.id,
        [glossary([row(STUDIES_LABEL, domain.studies), row(QUESTION_LABEL, domain.question)])],
        resolution,
    );
};

export const mathSection = function mathSection(reason: ReasonView, resolution: ResolutionView): Section {
    return {
        icon: REASON_SECTION_ICON,
        id: MATH_SECTION_ID,
        intro: MATH_INTRO,
        subsections: [
            ...reason.mathTypes.map((mathType) => mathTypeSubsection(mathType, resolution)),
            ...reason.mathDomains.map((domain) => mathDomainSubsection(domain, resolution)),
        ],
        title: MATH_TITLE,
    };
};

const lensSubsection = function lensSubsection(lens: LensView, resolution: ResolutionView): Subsection {
    const rows = [
        row(QUESTION_LABEL, lens.question),
        row(FIELDS_LABEL, plainList([...lens.mathDomains.map(linkOf), ...lens.mathFields])),
        row(SURFACES_LABEL, plainList(lens.surfaces)),
        row(DETECTORS_LABEL, linkList(lens.detectedBy)),
        ...linkRow(TEST_SURFACES_LABEL, lens.testSurfaces),
    ];
    return reasonRecord(
        lens.anchor,
        lens.label ?? lens.id,
        [chips([labeled(NATURE_LABEL, lens.nature), linked(UNIVERSAL_AXIS_LABEL, lens.universalAxis)]), glossary(rows)],
        resolution,
    );
};

export const lensSection = function lensSection(reason: ReasonView, resolution: ResolutionView): Section {
    return {
        icon: REASON_SECTION_ICON,
        id: LENS_SECTION_ID,
        intro: LENS_INTRO,
        subsections: reason.lenses.map((lens) => lensSubsection(lens, resolution)),
        title: LENS_TITLE,
    };
};

const surfaceSubsection = function surfaceSubsection(surface: TestSurfaceView, resolution: ResolutionView): Subsection {
    const evidence = fields([
        linkOf(surface.evidenceSource),
        surface.evidenceRequired ? REQUIRED_LABEL : OPTIONAL_LABEL,
    ]);
    const rows = [
        row(FIT_LABEL, surface.fit),
        row(FAILURE_MODES_LABEL, plainList(surface.failureModes)),
        row(TECHNIQUES_LABEL, linkList(surface.techniques)),
        ...linkRow(PREDICATE_GROUNDS_LABEL, surface.predicateGrounds),
        row(EVIDENCE_LABEL, evidence),
        ...linkRow(EVIDENCE_GROUNDS_LABEL, surface.evidenceGrounds),
        row(VERDICTS_LABEL, linkList(surface.verdictDomain)),
    ];
    return reasonRecord(
        surface.anchor,
        surface.id,
        [
            chips([
                linked(DIMENSION_LABEL, surface.dimension),
                linked(LENS_LABEL, surface.lens),
                linked(INVARIANT_LABEL, surface.invariant),
                linked(PREDICATE_TITLE, surface.predicateType),
            ]),
            glossary(rows),
            codeOf(PREDICATE_TITLE, surface.predicateExpression, TEXT_LANGUAGE),
        ],
        resolution,
    );
};

export const surfaceSection = function surfaceSection(reason: ReasonView, resolution: ResolutionView): Section {
    return {
        icon: REASON_SECTION_ICON,
        id: SURFACE_SECTION_ID,
        intro: SURFACE_INTRO,
        subsections: reason.testSurfaces.map((surface) => surfaceSubsection(surface, resolution)),
        title: SURFACE_TITLE,
    };
};