#!/usr/bin/env node
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
const inquirer_1 = __importDefault(require("inquirer"));
const appGenerator_1 = require("../src/generators/appGenerator");
const codeGenerator_1 = require("../src/generators/codeGenerator");
// ---------------------------------------------------------------------------
// Presets — opinionated bundles that skip the 12-question prompt
// ---------------------------------------------------------------------------
const PRESETS = {
    minimal: {
        typescript: true,
        absoluteImports: false,
        state: "None",
        auth: false,
        dataFetching: "None",
        validation: [],
        storage: "None",
        apiClient: false,
        ci: false,
        husky: false,
        sentry: false,
        i18n: false,
        maestro: false,
        nativewind: false,
    },
    fintech: {
        platform: "React Native CLI",
        typescript: true,
        absoluteImports: true,
        state: "Redux Toolkit",
        auth: true,
        dataFetching: "React Query",
        validation: [],
        storage: "MMKV",
        apiClient: true,
        apiClientType: "Fetch",
        ci: true,
        husky: true,
        sentry: true,
        i18n: true,
        maestro: true,
        nativewind: false,
    },
    social: {
        platform: "Expo",
        typescript: true,
        absoluteImports: true,
        state: "Zustand",
        auth: true,
        dataFetching: "SWR",
        validation: [],
        storage: "AsyncStorage",
        apiClient: true,
        apiClientType: "Fetch",
        ci: true,
        husky: true,
        sentry: true,
        i18n: true,
        maestro: false,
        nativewind: false,
    },
    indie: {
        platform: "Expo",
        typescript: true,
        absoluteImports: true,
        state: "Context API",
        auth: true,
        dataFetching: "React Query",
        validation: [],
        storage: "AsyncStorage",
        apiClient: true,
        apiClientType: "Fetch",
        ci: false,
        husky: true,
        sentry: false,
        i18n: false,
        maestro: false,
        nativewind: false,
    },
};
// ---------------------------------------------------------------------------
// Interactive prompt
// ---------------------------------------------------------------------------
async function runInteractivePrompt(prefilledName, presetDefaults) {
    const answers = await inquirer_1.default.prompt([
        {
            type: "input",
            name: "projectName",
            message: "Project name:",
            when: () => !prefilledName,
            validate: (input) => /^[A-Za-z0-9]+$/.test(input)
                ? true
                : "Project name must be alphanumeric",
        },
        {
            type: "rawlist",
            name: "platform",
            message: "Choose platform",
            choices: ["Expo", "React Native CLI"],
            when: () => !presetDefaults?.platform,
        },
        {
            type: "confirm",
            name: "typescript",
            message: "Use TypeScript?",
            default: true,
            when: () => presetDefaults?.typescript === undefined,
        },
        {
            type: "confirm",
            name: "absoluteImports",
            message: "Use absolute import alias (@/*)?",
            default: true,
            when: () => presetDefaults?.absoluteImports === undefined,
        },
        {
            type: "rawlist",
            name: "state",
            message: "State management?",
            choices: ["None", "Context API", "Redux Toolkit", "Zustand"],
            default: "Context API",
            when: () => !presetDefaults?.state,
        },
        {
            type: "rawlist",
            name: "dataFetching",
            message: "Data fetching library?",
            choices: ["None", "React Query", "SWR"],
            default: "None",
            when: () => !presetDefaults?.dataFetching,
        },
        {
            type: "checkbox",
            name: "validation",
            message: "Validation libraries?",
            choices: ["Formik", "React Hook Form", "Yup"],
            default: [],
            when: () => !presetDefaults?.validation?.length,
            validate: (selected) => {
                if (selected.includes("Formik") && !selected.includes("Yup")) {
                    return "Formik requires Yup in this starter. Select Yup as well.";
                }
                return true;
            },
        },
        {
            type: "rawlist",
            name: "storage",
            message: "Storage method?",
            choices: ["AsyncStorage", "MMKV", "None"],
            default: "AsyncStorage",
            when: () => !presetDefaults?.storage,
        },
        {
            type: "confirm",
            name: "auth",
            message: "Include auth scaffold?",
            default: false,
            when: () => presetDefaults?.auth === undefined,
        },
        {
            type: "confirm",
            name: "apiClient",
            message: "Include API client scaffold?",
            default: true,
            when: () => presetDefaults?.apiClient === undefined,
        },
        {
            type: "rawlist",
            name: "apiClientType",
            message: "API client transport?",
            choices: ["Fetch", "Axios"],
            default: "Fetch",
            when: (a) => presetDefaults?.apiClientType === undefined && a.apiClient,
        },
        {
            type: "confirm",
            name: "sentry",
            message: "Include Sentry crash reporting?",
            default: false,
            when: () => presetDefaults?.sentry === undefined,
        },
        {
            type: "confirm",
            name: "i18n",
            message: "Include i18n (i18next + react-i18next)?",
            default: false,
            when: () => presetDefaults?.i18n === undefined,
        },
        {
            type: "confirm",
            name: "maestro",
            message: "Include Maestro E2E test flows?",
            default: false,
            when: () => presetDefaults?.maestro === undefined,
        },
        {
            type: "confirm",
            name: "nativewind",
            message: "Use NativeWind (Tailwind CSS for React Native)?",
            default: false,
            when: () => presetDefaults?.nativewind === undefined,
        },
        {
            type: "confirm",
            name: "ci",
            message: "Include CI setup (GitHub Actions)?",
            default: false,
            when: () => presetDefaults?.ci === undefined,
        },
        {
            type: "confirm",
            name: "husky",
            message: "Set up Husky pre-commit hooks (lint-staged)?",
            default: true,
            when: () => presetDefaults?.husky === undefined,
        },
    ]);
    return {
        projectName: prefilledName ?? answers.projectName,
        platform: answers.platform ?? presetDefaults?.platform ?? "Expo",
        typescript: answers.typescript ?? presetDefaults?.typescript ?? true,
        absoluteImports: answers.absoluteImports ?? presetDefaults?.absoluteImports ?? true,
        state: answers.state ?? presetDefaults?.state ?? "None",
        auth: answers.auth ?? presetDefaults?.auth ?? false,
        dataFetching: answers.dataFetching ?? presetDefaults?.dataFetching ?? "None",
        validation: answers.validation ?? presetDefaults?.validation ?? [],
        storage: answers.storage ?? presetDefaults?.storage ?? "AsyncStorage",
        apiClient: answers.apiClient ?? presetDefaults?.apiClient ?? false,
        apiClientType: answers.apiClientType ?? presetDefaults?.apiClientType,
        sentry: answers.sentry ?? presetDefaults?.sentry ?? false,
        i18n: answers.i18n ?? presetDefaults?.i18n ?? false,
        maestro: answers.maestro ?? presetDefaults?.maestro ?? false,
        nativewind: answers.nativewind ?? presetDefaults?.nativewind ?? false,
        ci: answers.ci ?? presetDefaults?.ci ?? false,
        husky: answers.husky ?? presetDefaults?.husky ?? true,
    };
}
// ---------------------------------------------------------------------------
// CLI wiring
// ---------------------------------------------------------------------------
const program = new commander_1.Command();
program
    .name("create-rnstarterkit")
    .description("Scaffold a production-ready React Native app")
    .version("1.0.0")
    .argument("[projectName]", "Name of the project (alphanumeric)")
    .option("--preset <name>", "Skip prompts with a preset: minimal | fintech | social | indie")
    .action(async (projectName, cmdOptions) => {
    console.log("🚀 Welcome to RNStarterKit!");
    const presetKey = cmdOptions.preset;
    if (presetKey && !(presetKey in PRESETS)) {
        console.error(`❌ Unknown preset "${presetKey}". Available: ${Object.keys(PRESETS).join(", ")}`);
        process.exit(1);
    }
    const presetDefaults = presetKey ? PRESETS[presetKey] : undefined;
    // Preset + name supplied → fully non-interactive
    if (presetDefaults && projectName) {
        const options = {
            projectName,
            platform: presetDefaults.platform ?? "Expo",
            typescript: presetDefaults.typescript ?? true,
            absoluteImports: presetDefaults.absoluteImports ?? true,
            state: presetDefaults.state ?? "None",
            auth: presetDefaults.auth ?? false,
            dataFetching: presetDefaults.dataFetching ?? "None",
            validation: presetDefaults.validation ?? [],
            storage: presetDefaults.storage ?? "AsyncStorage",
            apiClient: presetDefaults.apiClient ?? false,
            apiClientType: presetDefaults.apiClientType,
            sentry: presetDefaults.sentry ?? false,
            i18n: presetDefaults.i18n ?? false,
            maestro: presetDefaults.maestro ?? false,
            nativewind: presetDefaults.nativewind ?? false,
            ci: presetDefaults.ci ?? false,
            husky: presetDefaults.husky ?? true,
        };
        console.log(`⚡ Using preset: ${presetKey}`);
        await (0, appGenerator_1.generateApp)(options);
    }
    else {
        const options = await runInteractivePrompt(projectName, presetDefaults);
        await (0, appGenerator_1.generateApp)(options);
    }
});
// generate subcommand
program
    .command("generate <type> <name>")
    .description("Generate code inside an existing rnstarterkit project\n" +
    "  Types: screen | component | hook | slice")
    .action(async (type, name) => {
    await (0, codeGenerator_1.generateCode)(type, name);
});
program.parseAsync(process.argv).catch((error) => {
    console.error("❌ Failed.");
    console.error(error);
    process.exit(1);
});
