import { renderInputMatchTypeVariants } from "./match-literals.js";
import { isValidIdentifier, quotePropertyKey } from "./variable-access.js";
export function jsDocBundleFunctionTypes(args) {
    const localesUnion = args.locales.map((locale) => `"${locale}"`).join(" | ");
    const inputType = args.inputTypeOverride ?? inputsType(args.inputs, args.matchTypes);
    return `
* @param {${inputType}} inputs
* @param {{ locale?: ${localesUnion} }} options
* @returns {LocalizedString}`;
}
/**
 * Returns the types for the input variables.
 *
 * @example
 *   const inputs = [{ name: "age" }]
 *   inputsType(inputs)
 *   >> "{ age: NonNullable<unknown> }"
 */
export function inputsType(inputs, matchTypes) {
    if (inputs.length === 0) {
        return "{}";
    }
    // Deduplicate inputs by name to avoid TypeScript errors with duplicate properties in JSDoc
    const uniqueInputMap = new Map();
    for (const input of inputs) {
        uniqueInputMap.set(input.name, input);
    }
    const uniqueInputs = Array.from(uniqueInputMap.values());
    const inputParams = uniqueInputs
        .map((input) => {
        const name = isValidIdentifier(input.name)
            ? input.name
            : quotePropertyKey(input.name);
        return `${name}: ${resolveInputType(input.name, matchTypes)}`;
    })
        .join(", ");
    return `{ ${inputParams} }`;
}
function resolveInputType(name, matchTypes) {
    if (!matchTypes)
        return "NonNullable<unknown>";
    const info = matchTypes.get(name);
    if (!info)
        return "NonNullable<unknown>";
    if (info.hasCatchAll)
        return "NonNullable<unknown>";
    const literals = Array.from(info.literals);
    if (literals.length === 0)
        return "NonNullable<unknown>";
    literals.sort();
    return literals
        .flatMap((value) => renderInputMatchTypeVariants(value))
        .filter((value, index, values) => values.indexOf(value) === index)
        .join(" | ");
}
export function inputTypeForName(name, matchTypes) {
    return resolveInputType(name, matchTypes);
}
//# sourceMappingURL=jsdoc-types.js.map