"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = update;
const devkit_1 = require("@nx/devkit");
const fs_1 = require("fs");
const path_1 = require("path");
async function update(tree) {
    devkit_1.logger.info('🔄 Adding GitHub documentation workflow...');
    if (!tree.exists('.unisphere')) {
        devkit_1.logger.info('⏭️ No .unisphere file found, skipping');
        return;
    }
    const config = (0, devkit_1.readJson)(tree, '.unisphere');
    const experienceName = config.name;
    if (!experienceName) {
        devkit_1.logger.info('⏭️ No experience name found in .unisphere, skipping');
        return;
    }
    if (!config.elements?.documentation || Object.keys(config.elements.documentation).length === 0) {
        devkit_1.logger.info('⏭️ No documentation elements found in .unisphere, skipping');
        return;
    }
    if (!tree.exists('.github')) {
        tree.write('.github/.gitkeep', '');
        devkit_1.logger.info('✅ Created .github directory');
    }
    if (!tree.exists('.github/workflows')) {
        tree.write('.github/workflows/.gitkeep', '');
        devkit_1.logger.info('✅ Created .github/workflows directory');
    }
    try {
        const workflowPath = '.github/workflows/documentation.yml';
        const templatePath = (0, path_1.join)(__dirname, 'templates', 'documentation-workflow.template');
        const templateContent = (0, fs_1.readFileSync)(templatePath, 'utf-8');
        const workflowContent = templateContent.replace(/__EXPERIENCE_NAME__/g, experienceName);
        const workflowExists = tree.exists(workflowPath);
        tree.write(workflowPath, workflowContent);
        if (workflowExists) {
            devkit_1.logger.info(`✅ Updated ${workflowPath}`);
        }
        else {
            devkit_1.logger.info(`✅ Created ${workflowPath}`);
        }
    }
    catch (error) {
        devkit_1.logger.error(`❌ Failed to add documentation workflow: ${error?.message || 'Unknown error'}`);
        throw error;
    }
}
