import JDLUnaryOption from '../../models/jdl-unary-option.js';
import JDLBinaryOption from '../../models/jdl-binary-option.js';
import { unaryOptions, binaryOptions } from '../../jhipster/index.mjs';
const { OptionValues, getOptionName } = binaryOptions;
export default { convertOptions };
export function convertOptions(parsedOptions, useOptions) {
    if (!parsedOptions) {
        throw new Error('Options have to be passed so as to be converted.');
    }
    const convertedUnaryOptions = convertUnaryOptions(parsedOptions);
    const convertedBinaryOptions = convertBinaryOptions(parsedOptions);
    const convertedUseOptions = convertUseOptions(useOptions);
    return [...convertedUnaryOptions, ...convertedBinaryOptions, ...convertedUseOptions];
}
function convertUnaryOptions(parsedOptions) {
    const convertedUnaryOptions = [];
    unaryOptions.forEach(unaryOptionName => {
        const parsedUnaryOption = parsedOptions[unaryOptionName];
        if (!parsedUnaryOption || !parsedUnaryOption.list || parsedUnaryOption.list.length === 0) {
            return;
        }
        convertedUnaryOptions.push(new JDLUnaryOption({
            name: unaryOptionName,
            entityNames: parsedUnaryOption.list,
            excludedNames: parsedUnaryOption.excluded,
        }));
    });
    return convertedUnaryOptions;
}
function convertBinaryOptions(parsedOptions) {
    const convertedBinaryOptions = [];
    binaryOptions.forEach(binaryOptionName => {
        if (!parsedOptions[binaryOptionName]) {
            return;
        }
        const optionValues = Object.keys(parsedOptions[binaryOptionName]);
        optionValues.forEach(optionValue => {
            const parsedBinaryOption = parsedOptions[binaryOptionName][optionValue];
            convertedBinaryOptions.push(new JDLBinaryOption({
                name: binaryOptionName,
                value: optionValue,
                entityNames: parsedBinaryOption.list,
                excludedNames: parsedBinaryOption.excluded,
            }));
        });
    });
    return convertedBinaryOptions;
}
function convertUseOptions(useOptions) {
    const convertedUseOptions = [];
    useOptions.forEach(useValue => {
        const { optionValues, list, excluded } = useValue;
        optionValues.forEach(optionValue => {
            if (!OptionValues[optionValue]) {
                return;
            }
            convertedUseOptions.push(new JDLBinaryOption({
                name: getOptionName(OptionValues[optionValue]),
                value: optionValue,
                entityNames: list,
                excludedNames: excluded,
            }));
        });
    });
    return convertedUseOptions;
}
