"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
async function update(tree) {
    devkit_1.logger.info('🔄 Removing Storybook files and dependencies...');
    let removedItems = 0;
    // 1. Remove storybook folder if it exists
    if (tree.exists('storybook')) {
        tree.delete('storybook');
        devkit_1.logger.info('📁 Removed storybook folder');
        removedItems++;
    }
    // 2. Remove storybook dependencies from package.json
    const packageJsonPath = 'package.json';
    if (tree.exists(packageJsonPath)) {
        try {
            (0, devkit_1.updateJson)(tree, packageJsonPath, (packageJson) => {
                let dependenciesRemoved = 0;
                // Remove from dependencies
                if (packageJson.dependencies) {
                    const depsToRemove = Object.keys(packageJson.dependencies).filter(dep => dep === 'storybook' || dep.startsWith('@storybook/') || dep === '@nx/storybook');
                    depsToRemove.forEach(dep => {
                        delete packageJson.dependencies[dep];
                        dependenciesRemoved++;
                    });
                }
                // Remove from devDependencies
                if (packageJson.devDependencies) {
                    const devDepsToRemove = Object.keys(packageJson.devDependencies).filter(dep => dep === 'storybook' || dep.startsWith('@storybook/'));
                    devDepsToRemove.forEach(dep => {
                        delete packageJson.devDependencies[dep];
                        dependenciesRemoved++;
                    });
                }
                // Remove from peerDependencies
                if (packageJson.peerDependencies) {
                    const peerDepsToRemove = Object.keys(packageJson.peerDependencies).filter(dep => dep === 'storybook' || dep.startsWith('@storybook/'));
                    peerDepsToRemove.forEach(dep => {
                        delete packageJson.peerDependencies[dep];
                        dependenciesRemoved++;
                    });
                }
                if (dependenciesRemoved > 0) {
                    devkit_1.logger.info(`📦 Removed ${dependenciesRemoved} Storybook dependencies from package.json`);
                    removedItems++;
                }
                return packageJson;
            });
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Unknown error';
            devkit_1.logger.warn(`⚠️ Could not update package.json: ${errorMessage}`);
            throw new Error(`⚠️ Could not update package.json: ${errorMessage}`);
        }
    }
    // 3. Remove storybook paths from tsconfig.base.json
    const tsconfigPath = 'tsconfig.base.json';
    if (tree.exists(tsconfigPath)) {
        try {
            (0, devkit_1.updateJson)(tree, tsconfigPath, (tsconfig) => {
                let pathsRemoved = 0;
                if (tsconfig.compilerOptions?.paths) {
                    const pathsToRemove = Object.entries(tsconfig.compilerOptions.paths).filter(([key, paths]) => paths.some(path => path.startsWith('storybook/'))).map(([key]) => key);
                    pathsToRemove.forEach(key => {
                        delete tsconfig.compilerOptions.paths[key];
                        pathsRemoved++;
                    });
                    if (pathsRemoved > 0) {
                        devkit_1.logger.info(`� Removed ${pathsRemoved} Storybook paths from tsconfig.base.json`);
                        removedItems++;
                    }
                }
                return tsconfig;
            });
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Unknown error';
            throw new Error(`⚠️ Could not update tsconfig.base.json: ${errorMessage}`);
        }
    }
    // 4. Remove @nx/storybook plugin from nx.json
    const nxJsonPath = 'nx.json';
    if (tree.exists(nxJsonPath)) {
        try {
            (0, devkit_1.updateJson)(tree, nxJsonPath, (nxJson) => {
                let pluginsRemoved = 0;
                if (nxJson.plugins && Array.isArray(nxJson.plugins)) {
                    const originalLength = nxJson.plugins.length;
                    nxJson.plugins = nxJson.plugins.filter((plugin) => {
                        const pluginName = typeof plugin === 'string' ? plugin : plugin.plugin;
                        return pluginName !== '@nx/storybook/plugin' && pluginName !== '@nx/storybook';
                    });
                    pluginsRemoved = originalLength - nxJson.plugins.length;
                    if (pluginsRemoved > 0) {
                        devkit_1.logger.info(`🔌 Removed ${pluginsRemoved} Storybook plugin(s) from nx.json`);
                        removedItems++;
                    }
                }
                return nxJson;
            });
        }
        catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Unknown error';
            throw new Error(`⚠️ Could not update nx.json: ${errorMessage}`);
        }
    }
    if (removedItems === 0) {
        devkit_1.logger.info('ℹ️ No Storybook files, dependencies, or paths found to remove');
    }
    else {
        devkit_1.logger.info(`✅ Successfully removed Storybook components (${removedItems} operations completed)`);
    }
}
