/**
 * Copyright 2013-2026 the original author or authors from the JHipster project.
 *
 * This file is part of the JHipster project, see https://www.jhipster.tech/
 * for more information.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
    if (typeof path === "string" && /^\.\.?\//.test(path)) {
        return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
            return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
        });
    }
    return path;
};
import { readFile, readdir } from 'node:fs/promises';
import { extname, join } from 'node:path';
import { getUnknownGitHubMatrixGroupProperties } from "./github-matrix.js";
export const getGithubSamplesGroups = async (samplesGroupFolder, keepExtensions = false) => {
    const samplesFolderContent = await readdir(samplesGroupFolder);
    return samplesFolderContent
        .filter(sample => !sample.startsWith('_') && ['.json', '.js', '.ts', ''].includes(extname(sample)))
        .map(sample => (keepExtensions ? sample : sample.split('.')[0]));
};
export const getGithubSamplesGroup = async (samplesGroupFolder, group) => {
    const warnings = [];
    let samples = {};
    const samplesFolderContent = await getGithubSamplesGroups(samplesGroupFolder, true);
    const groupExt = ['js', 'ts', 'json'].find(ext => samplesFolderContent.includes(`${group}.${ext}`));
    if (groupExt === 'js' || groupExt === 'ts') {
        const jsGroup = await import(__rewriteRelativeImportExtension(join(samplesGroupFolder, `${group}.${groupExt}`)));
        samples = Object.fromEntries(Object.entries(jsGroup.default).map(([sample, value]) => [sample, { ...value, 'samples-group': group }]));
    }
    else if (groupExt === 'json') {
        const jsonFile = await readFile(join(samplesGroupFolder, `${group}.json`));
        samples = Object.fromEntries(Object.entries(JSON.parse(jsonFile.toString())).map(([sample, value]) => [
            sample,
            { ...value, 'samples-group': group },
        ]));
    }
    else if (samplesFolderContent.includes(group)) {
        const samplesFolderContent = await readdir(join(samplesGroupFolder, group));
        if (samplesFolderContent.length > 0) {
            // Support jdl files and folder (with .yo-rc.json files)
            samples = Object.fromEntries(samplesFolderContent
                .filter(sample => ['', '.jdl'].includes(extname(sample)))
                .map(sample => [
                sample.replace('.jdl', ''),
                {
                    'samples-group': group,
                    'sample-type': extname(sample) === '.jdl' ? 'jdl' : 'yo-rc',
                },
            ]));
            if (samplesFolderContent.includes('samples.json')) {
                const jsonFile = await readFile(join(samplesGroupFolder, group, 'samples.json'));
                const jsonSamples = JSON.parse(jsonFile.toString());
                for (const [sample, value] of Object.entries(jsonSamples)) {
                    if (!samples[sample]) {
                        throw new Error(`Sample ${sample} not found in ${group}`);
                    }
                    samples[sample] = { ...samples[sample], ...value };
                }
            }
        }
    }
    if (!samples) {
        throw new Error();
    }
    const unknownProperties = getUnknownGitHubMatrixGroupProperties(samples);
    if (unknownProperties.length) {
        warnings.push(`Unknown properties in ${group}: ${unknownProperties.join(', ')}`);
    }
    return { samples, warnings };
};
