import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { map, prop, uniqBy } from 'rambda';
const featureToggles = readFile(join(process.cwd(), 'ft.json'), 'utf8')
    .then(json => JSON.parse(json))
    .then(map(ft => ({
    ...ft,
    created: new Date(ft.created),
    expired: ft.expired ? new Date(ft.expired) : undefined,
    updated: ft.updated ? new Date(ft.updated) : undefined,
})))
    // eslint-disable-next-line unicorn/prefer-top-level-await
    .catch(() => []);
const featureTogglesByRepoId = featureToggles
    // eslint-disable-next-line unicorn/prefer-top-level-await
    .then(fts => fts.reduce((acc, ft) => {
    ft.repoIds.forEach(repoId => {
        acc[repoId] = acc[repoId] || [];
        acc[repoId].push(ft);
    });
    return acc;
}, {}));
export const featureTogglesForRepos = async (repoIds) => {
    const ftByRepoId = await featureTogglesByRepoId;
    return uniqBy(prop('featureId'), repoIds.reduce((acc, repoId) => {
        acc.push(...(ftByRepoId[repoId] || []));
        return acc;
    }, []));
};
