import { compile } from 'ass-compiler';

import { processDialogues } from './cue-processor';
import { IdGenerator } from './id-generator';
import { generateMetaHCL } from './meta-processor';
import { processStyles } from './style-processor';

/**
 * Converts a string of ASS subtitle content into ESL (Example Subtitle Language) format.
 *
 * This function processes the `info`, `styles`, and `dialogues` sections of the ASS file,
 * converting them into their ESL equivalents.
 *
 * @param assContent - The full string content of the ASS file.
 * @returns The generated ESL content as a single string.
 *
 */
export default function convertASStoESL(assContent: string): string {
    const compiledASS = compile(assContent, {});

    const motionIdGenerator = new IdGenerator();

    const metaHCL = generateMetaHCL(compiledASS.info);
    const { stylesHCL, processedStylesMap } = processStyles(compiledASS.styles);
    const { cuesHCL, motionsHCL, autoGeneratedStylesHCL } = processDialogues(
        compiledASS.dialogues,
        processedStylesMap,
        motionIdGenerator,
    );

    const finalEslContent = [metaHCL, stylesHCL, autoGeneratedStylesHCL, motionsHCL, cuesHCL]
        .filter(Boolean)
        .join('\n\n');

    return finalEslContent;
}
