"use strict";
/**
 * Import Resolver
 *
 * Builds import paths for selectors, models, and related modules.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildImportStatements = buildImportStatements;
exports.buildModelImports = buildModelImports;
exports.buildSelectorImports = buildSelectorImports;
exports.buildLibraryImports = buildLibraryImports;
exports.buildModuleImportPath = buildModuleImportPath;
exports.buildSelectorImportPath = buildSelectorImportPath;
exports.getWebBasePath = getWebBasePath;
exports.getPageBasePath = getPageBasePath;
exports.buildFilePaths = buildFilePaths;
exports.buildRelativeImport = buildRelativeImport;
const field_mapping_types_1 = require("../types/field-mapping.types");
const name_transformer_1 = require("./name-transformer");
/**
 * Build all import statements needed for a module
 *
 * @param relationships - Array of frontend relationships
 * @param extendsContent - Whether the module extends Content
 * @returns Import statements grouped by category
 */
function buildImportStatements(relationships, extendsContent) {
    return {
        models: buildModelImports(relationships),
        selectors: buildSelectorImports(relationships),
        library: buildLibraryImports(extendsContent),
    };
}
/**
 * Build model/interface import statements
 *
 * @param relationships - Array of frontend relationships
 * @returns Array of import statements for models
 */
function buildModelImports(relationships) {
    const imports = [];
    relationships.forEach((rel) => {
        // Import interface for relationships
        imports.push(`import { ${rel.interfaceName} } from "${rel.interfaceImportPath}";`);
    });
    return [...new Set(imports)]; // Deduplicate
}
/**
 * Build selector component import statements
 *
 * @param relationships - Array of frontend relationships
 * @returns Array of import statements for selectors
 */
function buildSelectorImports(relationships) {
    const imports = [];
    relationships.forEach((rel) => {
        // Skip author - handled specially or auto-set
        if (rel.variant === field_mapping_types_1.AUTHOR_VARIANT) {
            return;
        }
        // Foundation components use MultiSelect, generated modules use MultiSelector
        const componentName = rel.single
            ? `${rel.name}Selector`
            : (rel.isFoundation ? `${rel.name}MultiSelect` : `${rel.name}MultiSelector`);
        imports.push(`import ${componentName} from "${rel.importPath}";`);
    });
    return [...new Set(imports)]; // Deduplicate
}
/**
 * Build library import statements
 *
 * @param extendsContent - Whether the module extends Content
 * @returns Array of import statements for library
 */
function buildLibraryImports(extendsContent) {
    const imports = [];
    // Core imports always needed
    imports.push(`import { AbstractApiData } from "@carlonicora/nextjs-jsonapi/core";`);
    // Content import if extending
    if (extendsContent) {
        imports.push(`import { Content, ContentInterface, ContentInput } from "@carlonicora/nextjs-jsonapi/core";`);
    }
    return imports;
}
/**
 * Get the subdirectory path from targetDir, stripping the base "features" or "foundations" prefix
 * since the @/features/ alias already provides that base.
 *
 * @param targetDir - Target directory (e.g., "features/customer-management" or "features")
 * @returns Subdirectory path (e.g., "customer-management" or "")
 */
function getSubdirectoryPath(targetDir) {
    const segments = targetDir.split("/");
    // Remove the first segment (features or foundations) since @/features/ already includes it
    return segments.slice(1).join("/");
}
/**
 * Build import path for a module's own files
 *
 * @param targetDir - Target directory
 * @param moduleName - Module name (PascalCase)
 * @param fileName - File name without extension
 * @returns Import path
 */
function buildModuleImportPath(targetDir, moduleName, fileName) {
    const kebab = (0, name_transformer_1.toKebabCase)(moduleName);
    const subdir = getSubdirectoryPath(targetDir);
    const subdirPath = subdir ? `${subdir}/` : "";
    return `@/features/${subdirPath}${kebab}/data/${fileName}`;
}
/**
 * Build import path for a related module's selector
 *
 * @param rel - Frontend relationship
 * @returns Import path for selector
 */
function buildSelectorImportPath(rel) {
    const componentName = rel.single ? `${rel.name}Selector` : `${rel.name}MultiSelector`;
    return `@/features/${rel.directory}/${rel.modelKebab}/components/forms/${componentName}`;
}
/**
 * Get the web base path for generated files
 *
 * @param targetDir - Target directory (e.g., "features/customer-management" or "features")
 * @param moduleName - Module name (PascalCase)
 * @returns Base path for generated files
 */
function getWebBasePath(targetDir, moduleName) {
    const kebab = (0, name_transformer_1.toKebabCase)(moduleName);
    const subdir = getSubdirectoryPath(targetDir);
    const subdirPath = subdir ? `${subdir}/` : "";
    return `apps/web/src/features/${subdirPath}${kebab}`;
}
/**
 * Get the page path for generated pages
 *
 * @param pluralKebab - Plural kebab-case name (e.g., "comments")
 * @returns Base path for page files
 */
function getPageBasePath(pluralKebab) {
    return `apps/web/src/app/[locale]/(main)/(features)/${pluralKebab}`;
}
/**
 * Build all file paths for a module from template data
 *
 * @param data - Frontend template data
 * @param rootPath - Root path for the monorepo (optional, defaults to "")
 * @returns Object with all file paths
 */
function buildFilePaths(data, rootPath = "") {
    const { names, targetDir } = data;
    const prefix = rootPath ? `${rootPath}/` : "";
    const subdir = getSubdirectoryPath(targetDir);
    const subdirPath = subdir ? `${subdir}/` : "";
    const basePath = `${prefix}apps/web/src/features/${subdirPath}${names.kebabCase}`;
    const pagePath = `${prefix}apps/web/src/app/[locale]/(main)/(features)/${names.pluralKebab}`;
    return {
        // Data layer
        interface: `${basePath}/data/${names.pascalCase}Interface.ts`,
        model: `${basePath}/data/${names.pascalCase}.ts`,
        service: `${basePath}/data/${names.pascalCase}Service.ts`,
        fields: `${basePath}/data/${names.pascalCase}Fields.ts`,
        // Components
        editor: `${basePath}/components/forms/${names.pascalCase}Editor.tsx`,
        deleter: `${basePath}/components/forms/${names.pascalCase}Deleter.tsx`,
        selector: `${basePath}/components/forms/${names.pascalCase}Selector.tsx`,
        multiSelector: `${basePath}/components/forms/${names.pascalCase}MultiSelector.tsx`,
        list: `${basePath}/components/lists/${names.pascalCase}List.tsx`,
        details: `${basePath}/components/details/${names.pascalCase}Details.tsx`,
        content: `${basePath}/components/details/${names.pascalCase}Content.tsx`,
        container: `${basePath}/components/containers/${names.pascalCase}Container.tsx`,
        listContainer: `${basePath}/components/containers/${names.pascalCase}ListContainer.tsx`,
        // Context & Hooks
        context: `${basePath}/contexts/${names.pascalCase}Context.tsx`,
        tableHook: `${basePath}/hooks/use${names.pascalCase}TableStructure.tsx`,
        // Module
        module: `${basePath}/${names.pascalCase}Module.ts`,
        // Pages
        listPage: `${pagePath}/page.tsx`,
        detailPage: `${pagePath}/[id]/page.tsx`,
    };
}
/**
 * Build relative import path between two files
 *
 * @param fromPath - Source file path
 * @param toPath - Target file path
 * @returns Relative import path
 */
function buildRelativeImport(fromPath, toPath) {
    // For files in same feature, use @ alias
    if (toPath.startsWith("@/")) {
        return toPath;
    }
    // Calculate relative path
    const fromParts = fromPath.split("/");
    const toParts = toPath.split("/");
    // Find common prefix
    let commonLength = 0;
    for (let i = 0; i < Math.min(fromParts.length, toParts.length); i++) {
        if (fromParts[i] === toParts[i]) {
            commonLength++;
        }
        else {
            break;
        }
    }
    // Build relative path
    const upCount = fromParts.length - commonLength - 1; // -1 for the file itself
    const relativeParts = toParts.slice(commonLength);
    const prefix = upCount > 0 ? "../".repeat(upCount) : "./";
    return prefix + relativeParts.join("/");
}
//# sourceMappingURL=import-resolver.js.map