function asRecord(value) {
    return typeof value === "object" && value !== null
        ? value
        : null;
}
function readString(value) {
    return typeof value === "string" && value.trim() ? value.trim() : undefined;
}
function readNumber(value) {
    return typeof value === "number" && Number.isFinite(value)
        ? value
        : undefined;
}
export function getProjectTemplateContextSummary(metadata) {
    const root = asRecord(metadata);
    if (!root)
        return null;
    const additionalData = asRecord(root.additionalData);
    const projectTemplate = asRecord(additionalData?.projectTemplate ?? root.projectTemplate);
    const projectTemplateSummary = asRecord(additionalData?.projectTemplateSummary ?? root.projectTemplateSummary);
    const projectTemplateGoalSummary = asRecord(additionalData?.projectTemplateGoalSummary ??
        root.projectTemplateGoalSummary);
    const projectTemplateGoalDefinitions = additionalData?.projectTemplateGoalDefinitions ??
        root.projectTemplateGoalDefinitions;
    const id = readString(additionalData?.projectTemplateId ??
        root.projectTemplateId ??
        projectTemplate?.id);
    const name = readString(additionalData?.projectTemplateName ??
        root.projectTemplateName ??
        projectTemplate?.name);
    const description = readString(additionalData?.projectTemplateDescription ??
        root.projectTemplateDescription ??
        projectTemplate?.description);
    const taskCount = readNumber(projectTemplateSummary?.taskCount) ??
        (Array.isArray(projectTemplate?.taskTemplates)
            ? projectTemplate.taskTemplates.length
            : undefined);
    const goalCount = readNumber(projectTemplateSummary?.goalCount) ??
        readNumber(projectTemplateGoalSummary?.goalCount) ??
        (Array.isArray(projectTemplateGoalDefinitions)
            ? projectTemplateGoalDefinitions.length
            : undefined) ??
        (Array.isArray(projectTemplate?.goalTemplates)
            ? projectTemplate.goalTemplates.length
            : undefined);
    const selectedTaskTitle = readString(additionalData?.selectedTaskTemplateTitle ?? root.selectedTaskTemplateTitle);
    const selectedGoalTitle = readString(additionalData?.selectedGoalTemplateTitle ?? root.selectedGoalTemplateTitle);
    const isProjectTemplateContext = additionalData?.projectTemplateEditor === true ||
        root.projectTemplateEditor === true ||
        !!id ||
        !!name;
    if (!isProjectTemplateContext)
        return null;
    return {
        id,
        name,
        description,
        taskCount,
        goalCount,
        selectedTaskTitle,
        selectedGoalTitle,
    };
}
//# sourceMappingURL=projectTemplateContext.js.map