"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getConfig = getConfig;
exports.resolveConfigPaths = resolveConfigPaths;
exports.getDefaultConfig = getDefaultConfig;
exports.writeConfig = writeConfig;
const cosmiconfig_1 = require("cosmiconfig");
const zod_1 = require("zod");
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const configSchema = zod_1.z.object({
    style: zod_1.z.string().default('default'),
    rsc: zod_1.z.boolean().default(false),
    tsx: zod_1.z.boolean().default(true),
    basePath: zod_1.z.string().optional(), // Base path for components (e.g., 'src/', 'packages/ui/')
    tailwind: zod_1.z.object({
        config: zod_1.z.string().default('tailwind.config.js'),
        css: zod_1.z.string().default('src/app/globals.css'),
        baseColor: zod_1.z.string().default('slate'),
        cssVariables: zod_1.z.boolean().default(true),
        prefix: zod_1.z.string().default('').optional(),
    }).default({}),
    aliases: zod_1.z.object({
        components: zod_1.z.string().default('@formickComponents'),
        utils: zod_1.z.string().default('@formickUtils'),
        ui: zod_1.z.string().default('@formickComponents'),
        constants: zod_1.z.string().default('@formickConstants'),
        types: zod_1.z.string().default('@formickTypes'),
        hooks: zod_1.z.string().default('@formickHooks'),
        assets: zod_1.z.string().default('@formickAssets'),
        mocks: zod_1.z.string().default('@formickMocks'),
    }).default({}),
});
const DEFAULT_CONFIG = {
    style: 'default',
    rsc: false,
    tsx: true,
    // basePath is not set by default - must be asked
    tailwind: {
        config: 'tailwind.config.js',
        css: 'src/app/globals.css',
        baseColor: 'slate',
        cssVariables: true,
        prefix: '',
    },
    aliases: {
        components: '#formickComponents',
        utils: '#formickUtils',
        ui: '#formickComponents',
        constants: '#formickConstants',
        types: '#formickTypes',
        hooks: '#formickHooks',
        assets: '#formickAssets',
        mocks: '#formickMocks',
    },
};
async function getConfig(cwd) {
    const explorer = (0, cosmiconfig_1.cosmiconfig)('cloudwick-ui', {
        searchPlaces: [
            'cloudwick-ui.json',
            '.cloudwick-ui.json',
            'cloudwick-ui.config.js',
            'cloudwick-ui.config.ts',
        ],
    });
    try {
        const result = await explorer.search(cwd);
        if (!result) {
            return null;
        }
        return configSchema.parse(result.config);
    }
    catch (error) {
        throw new Error(`Invalid configuration: ${error}`);
    }
}
async function resolveConfigPaths(cwd, config) {
    const tailwindConfigPath = path_1.default.resolve(cwd, config.tailwind.config);
    const tailwindCssPath = path_1.default.resolve(cwd, config.tailwind.css);
    // Convert alias paths to actual file system paths
    const componentsPath = config.aliases.components.replace(/^[@#]/, 'src/').replace(/\/$/, '');
    const utilsPath = config.aliases.utils.replace(/^[@#]/, 'src/').replace(/\/$/, '');
    const uiPath = config.aliases.ui.replace(/^[@#]/, 'src/').replace(/\/$/, '');
    const constantsPath = config.aliases.constants.replace(/^[@#]/, 'src/').replace(/\/$/, '');
    const typesPath = config.aliases.types.replace(/^[@#]/, 'src/').replace(/\/$/, '');
    const hooksPath = config.aliases.hooks.replace(/^[@#]/, 'src/').replace(/\/$/, '');
    const assetsPath = config.aliases.assets.replace(/^[@#]/, 'src/').replace(/\/$/, '');
    const mocksPath = config.aliases.mocks.replace(/^[@#]/, 'src/').replace(/\/$/, '');
    return {
        ...config,
        resolvedPaths: {
            tailwindConfig: tailwindConfigPath,
            tailwindCss: tailwindCssPath,
            components: path_1.default.resolve(cwd, componentsPath),
            ui: path_1.default.resolve(cwd, uiPath),
            utils: path_1.default.resolve(cwd, utilsPath),
            constants: path_1.default.resolve(cwd, constantsPath),
            types: path_1.default.resolve(cwd, typesPath),
            hooks: path_1.default.resolve(cwd, hooksPath),
            assets: path_1.default.resolve(cwd, assetsPath),
            mocks: path_1.default.resolve(cwd, mocksPath),
        },
    };
}
function getDefaultConfig() {
    return DEFAULT_CONFIG;
}
async function writeConfig(cwd, config) {
    const configPath = path_1.default.resolve(cwd, 'cloudwick-ui.json');
    await fs_extra_1.default.writeJSON(configPath, config, { spaces: 2 });
}
//# sourceMappingURL=config.js.map