"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.addPlaygroundGenerator = addPlaygroundGenerator;
const tslib_1 = require("tslib");
const devkit_1 = require("@nx/devkit");
const path = tslib_1.__importStar(require("path"));
const utils_1 = require("../utils");
const VALID_ENGINES = ['vanilla-ts', 'vanilla-js', 'react'];
function validateOptions(options) {
    if (!options.name || options.name.trim() === '') {
        throw new Error(`Missing required option: 'name'\n` +
            `  Description: The name of the playground to create\n` +
            `  Type: string\n` +
            `  Pattern: ^[a-zA-Z][a-zA-Z0-9\\-\\s]*$\n` +
            `  Example: nx g @unisphere/nx:add-playground --name=my-demo --engine=react`);
    }
    const namePattern = /^[a-zA-Z][a-zA-Z0-9\-\s]*$/;
    if (!namePattern.test(options.name)) {
        throw new Error(`Invalid value '${options.name}' for option 'name'\n` +
            `  Pattern: ^[a-zA-Z][a-zA-Z0-9\\-\\s]*$\n` +
            `  Received: ${options.name}`);
    }
    if (!options.engine || !VALID_ENGINES.includes(options.engine)) {
        throw new Error(`Invalid value '${options.engine}' for option 'engine'\n` +
            `  Allowed values: ${VALID_ENGINES.join(', ')}\n` +
            `  Example: nx g @unisphere/nx:add-playground --name=demo --engine=react`);
    }
    if (options.group) {
        const groupPattern = /^[a-zA-Z0-9][a-zA-Z0-9\-_/]*$/;
        if (!groupPattern.test(options.group)) {
            throw new Error(`Invalid value '${options.group}' for option 'group'\n` +
                `  Group path must contain only letters, numbers, hyphens, underscores, and forward slashes\n` +
                `  Example: --group=getting-started or --group=advanced/hooks`);
        }
    }
}
async function addPlaygroundGenerator(tree, options) {
    validateOptions(options);
    // Ensure we're in a valid Unisphere workspace
    (0, utils_1.validateUnisphereConfig)(tree);
    const playgroundName = (0, devkit_1.names)(options.name).fileName;
    const engine = options.engine;
    // Build target path
    const groupPath = options.group ? options.group.replace(/^\/|\/$/g, '') : '';
    const targetDir = groupPath
        ? `unisphere/playgrounds/${groupPath}/${playgroundName}`
        : `unisphere/playgrounds/${playgroundName}`;
    // Check if already exists
    if (tree.exists(`${targetDir}/playground.json`)) {
        throw new Error(`Playground already exists at "${targetDir}".\n` +
            'Please choose a different name or remove the existing playground first.');
    }
    // Template variables
    const templateVars = {
        ...(0, utils_1.createNameTransforms)(playgroundName, 'playgroundName'),
        tmpl: '',
    };
    // Generate files from engine-specific templates
    (0, devkit_1.generateFiles)(tree, path.join(__dirname, 'templates', engine), targetDir, templateVars);
    await (0, devkit_1.formatFiles)(tree);
    return () => {
        devkit_1.logger.info('');
        devkit_1.logger.info('✅ Code playground created successfully!');
        devkit_1.logger.info('');
        devkit_1.logger.info(`📁 Location: ${targetDir}`);
        devkit_1.logger.info(`🔧 Engine: ${engine}`);
        if (groupPath) {
            devkit_1.logger.info(`📂 Group: ${groupPath}`);
        }
        devkit_1.logger.info('');
    };
}
exports.default = addPlaygroundGenerator;
