import { platform } from 'os';
import { normalizeLineEndings } from './contents.mjs';
const isWin32 = platform() === 'win32';
export function joinCallbacks(...callbacks) {
    return function (content, filePath) {
        if (isWin32 && content.match(/\r\n/)) {
            const removeSlashRSlashN = ct => normalizeLineEndings(ct, '\n');
            const addSlashRSlashN = ct => normalizeLineEndings(ct, '\r\n');
            callbacks = [removeSlashRSlashN, ...callbacks, addSlashRSlashN];
        }
        for (const callback of callbacks) {
            content = callback.call(this, content, filePath);
        }
        return content;
    };
}
export function addSectionsCondition(files, commonCondition) {
    return Object.fromEntries(Object.entries(files).map(([sectionName, sectionValue]) => {
        sectionValue = sectionValue.map(block => {
            const { condition } = block;
            let newCondition = commonCondition;
            if (typeof condition === 'function') {
                newCondition = (...args) => {
                    return commonCondition(...args) && condition(...args);
                };
            }
            else if (condition !== undefined) {
                newCondition = (...args) => commonCondition(...args) && condition;
            }
            block = {
                ...block,
                condition: newCondition,
            };
            return block;
        });
        return [sectionName, sectionValue];
    }));
}
export function mergeSections(...allFiles) {
    const generated = {};
    for (const files of allFiles) {
        for (const [sectionName, sectionValue] of Object.entries(files)) {
            generated[sectionName] = generated[sectionName] || [];
            generated[sectionName].push(...sectionValue);
        }
    }
    return generated;
}
