/** Replaces placeholder tokens in template filenames with bounded-context / module / property names. */
import { getAdditionalApiFileName, getAdditionalApiPaths, } from '../utils/additional-api.js';
import { toKebabCase } from '../utils/string-utils.js';
export function replaceFilename(name, opts = {}) {
    const { boundedContextName = '', boundedContextPrefix = '', boundedContextSuffix = '', currentProperty, moduleName = '', moduleNamePrefix = '', moduleNames = '', moduleNameSuffix = '', } = opts;
    if (name.endsWith('.eta'))
        name = name.replace(/\.eta$/, '');
    if (name.includes('__bounded_context_name__')) {
        if (!boundedContextName) {
            throw new Error('boundedContextName is required to replace __bounded_context_name__');
        }
        const prefix = boundedContextPrefix ? `${boundedContextPrefix}-` : '';
        const suffix = boundedContextSuffix ? `-${boundedContextSuffix}` : '';
        name = name.replaceAll('__bounded_context_name__', `${prefix}${toKebabCase(boundedContextName)}${suffix}`);
    }
    if (name.includes('__module_name__')) {
        if (!moduleName) {
            throw new Error('moduleName is required to replace __module_name__');
        }
        const prefix = moduleNamePrefix ? `${moduleNamePrefix}-` : '';
        const suffix = moduleNameSuffix ? `-${moduleNameSuffix}` : '';
        name = name.replaceAll('__module_name__', `${prefix}${toKebabCase(moduleName)}${suffix}`);
    }
    if (name.includes('__module_names__')) {
        if (!moduleNames) {
            throw new Error('moduleNames is required to replace __module_names__');
        }
        name = name.replaceAll('__module_names__', toKebabCase(moduleNames));
    }
    if (name.includes('__property_name__') && currentProperty) {
        const propName = currentProperty.relationship?.type === 'many-to-many'
            ? `${currentProperty.relationship.singularName}Ids`
            : currentProperty.name;
        name = name.replaceAll('__property_name__', toKebabCase(propName));
    }
    if (name.includes('__property_origin_name__') && currentProperty) {
        name = name.replaceAll('__property_origin_name__', toKebabCase(currentProperty.name));
    }
    if (name.includes('__property_relationship_singular_name__') &&
        currentProperty?.relationship?.singularName) {
        name = name.replaceAll('__property_relationship_singular_name__', toKebabCase(currentProperty.relationship.singularName));
    }
    if (name.includes('__additional_api_name__') && opts.currentAdditionalApi) {
        name = name.replaceAll('__additional_api_name__', getAdditionalApiFileName(opts.currentAdditionalApi));
    }
    if (name.includes('__additional_api_action__') && opts.currentAdditionalApi) {
        const { pathAction } = getAdditionalApiPaths(opts.currentAdditionalApi);
        name = name.replaceAll('__additional_api_action__', pathAction);
    }
    return name;
}
