"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const InteropService_Exporter_Md_1 = require("./InteropService_Exporter_Md");
const BaseModel_1 = require("../../BaseModel");
const NoteTag_1 = require("../../models/NoteTag");
const Tag_1 = require("../../models/Tag");
const shim_1 = require("../../shim");
const types_1 = require("../database/types");
const frontMatter_1 = require("../../utils/frontMatter");
const Logger_1 = require("@joplin/utils/Logger");
const yaml = require("js-yaml");
const logger = Logger_1.default.create('InteropService_Exporter_Md_frontmatter');
class InteropService_Exporter_Md_frontmatter extends InteropService_Exporter_Md_1.default {
    // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Old code before rule was applied
    async prepareForProcessingItemType(itemType, itemsToExport) {
        await super.prepareForProcessingItemType(itemType, itemsToExport);
        if (itemType === BaseModel_1.default.TYPE_NOTE_TAG) {
            // Get tag list for each note
            const context = {
                noteTags: {},
            };
            for (let i = 0; i < itemsToExport.length; i++) {
                const it = itemsToExport[i].type;
                if (it !== itemType)
                    continue;
                const itemOrId = itemsToExport[i].itemOrId;
                const noteTag = typeof itemOrId === 'object' ? itemOrId : await NoteTag_1.default.load(itemOrId);
                if (!noteTag)
                    continue;
                if (!context.noteTags[noteTag.note_id])
                    context.noteTags[noteTag.note_id] = [];
                context.noteTags[noteTag.note_id].push(noteTag.tag_id);
            }
            this.updateContext(context);
        }
        else if (itemType === BaseModel_1.default.TYPE_TAG) {
            // Map tag ID to title
            const context = {
                tagTitles: {},
            };
            for (let i = 0; i < itemsToExport.length; i++) {
                const it = itemsToExport[i].type;
                if (it !== itemType)
                    continue;
                const itemOrId = itemsToExport[i].itemOrId;
                const tag = typeof itemOrId === 'object' ? itemOrId : await Tag_1.default.load(itemOrId);
                if (!tag)
                    continue;
                context.tagTitles[tag.id] = tag.title;
            }
            this.updateContext(context);
        }
    }
    // eslint-disable-next-line @typescript-eslint/no-explicit-any -- Matches parent class signature
    async processItem(itemType, item) {
        await super.processItem(itemType, item);
        // Write _folder.yml with folder icon when processing a folder
        if (item.type_ === BaseModel_1.ModelType.Folder && item.icon) {
            try {
                const icon = JSON.parse(item.icon);
                const dirPath = `${this.destDir_}/${await this.makeDirPath_(item)}`;
                const iconObj = this.serializeFolderIcon(icon);
                if (iconObj) {
                    const metadataPath = `${dirPath}_folder.yml`;
                    const yamlContent = yaml.dump({ icon: iconObj }, { noCompatMode: true, schema: yaml.FAILSAFE_SCHEMA });
                    await shim_1.default.fsDriver().writeFile(metadataPath, yamlContent, 'utf-8');
                }
            }
            catch (e) {
                logger.warn(`Failed to export folder icon for folder ${item.id}:`, e);
            }
        }
    }
    serializeFolderIcon(icon) {
        switch (icon.type) {
            case types_1.FolderIconType.Emoji:
                if (!icon.emoji)
                    return null;
                return { type: 'emoji', emoji: icon.emoji };
            case types_1.FolderIconType.FontAwesome:
                if (!icon.name)
                    return null;
                return { type: 'fontawesome', name: icon.name };
            case types_1.FolderIconType.DataUrl:
                if (!icon.dataUrl)
                    return null;
                return { type: 'dataurl', dataurl: icon.dataUrl };
            default: {
                const exhaustivenessCheck = icon.type;
                throw new Error(`Unknown folder icon type: ${exhaustivenessCheck}`);
            }
        }
    }
    async getNoteExportContent_(modNote) {
        let tagTitles = [];
        const context = this.context();
        if (context.noteTags[modNote.id]) {
            const tagIds = context.noteTags[modNote.id];
            // In some cases a NoteTag can still exist, while the Tag does not. In this case, tagTitles
            // for that tagId will return undefined, which can't be handled by the yaml library (issue #7782)
            tagTitles = tagIds.map((id) => context.tagTitles[id]).filter(e => !!e).sort();
        }
        return (0, frontMatter_1.serialize)(modNote, tagTitles);
    }
}
exports.default = InteropService_Exporter_Md_frontmatter;
//# sourceMappingURL=InteropService_Exporter_Md_frontmatter.js.map