"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const chalk_1 = __importDefault(require("chalk"));
const path_1 = __importStar(require("path"));
const inquirer_1 = __importDefault(require("inquirer"));
const prompts_1 = require("./prompts");
const common_1 = require("../../common");
const remove_dev_dependencies_1 = require("./remove-dev-dependencies");
inquirer_1.default.registerPrompt('nextjs-checkbox', prompts_1.NextjsCheckbox);
var PlatformCompatibility;
(function (PlatformCompatibility) {
    PlatformCompatibility[PlatformCompatibility["SXP"] = 0] = "SXP";
    PlatformCompatibility[PlatformCompatibility["XMC"] = 1] = "XMC";
    PlatformCompatibility[PlatformCompatibility["Both"] = 2] = "Both";
})(PlatformCompatibility || (PlatformCompatibility = {}));
const addOnChoices = [
    {
        name: 'nextjs-styleguide - Includes example components and setup for working disconnected',
        value: 'nextjs-styleguide',
        platform: PlatformCompatibility.Both,
    },
    {
        name: 'nextjs-styleguide-tracking - Includes example (Sitecore XP) tracking component',
        value: 'nextjs-styleguide-tracking',
        platform: PlatformCompatibility.SXP,
    },
    {
        name: 'nextjs-sxa - Includes example components and setup for Headless SXA projects',
        value: 'nextjs-sxa',
        platform: PlatformCompatibility.Both,
    },
    {
        name: 'nextjs-multisite - Includes example setup for hosting multiple sites in a single NextJS application',
        value: 'nextjs-multisite',
        platform: PlatformCompatibility.Both,
    },
];
class NextjsInitializer {
    get isBase() {
        return true;
    }
    init(args) {
        return __awaiter(this, void 0, void 0, function* () {
            const answers = yield inquirer_1.default.prompt(prompts_1.prompts, args);
            const pkgPath = path_1.default.resolve(`${answers.destination}${path_1.sep}package.json`);
            const templatePath = path_1.default.resolve(__dirname, '../../templates/nextjs');
            yield (0, common_1.transform)(templatePath, Object.assign(Object.assign({}, args), answers));
            if (!(0, common_1.isDevEnvironment)(args.destination)) {
                (0, remove_dev_dependencies_1.removeDevDependencies)(args.destination);
            }
            const addInitializers = [];
            if (answers.xmcloud && !args.templates.includes('nextjs-xmcloud')) {
                // add the "system" nextjs-xmcloud template if needed
                addInitializers.push('nextjs-xmcloud');
            }
            // don't prompt for add-on initializers if --yes or they've already specified
            // multiple via --templates (assume they know what they're doing)
            if (!args.yes && args.templates.length === 1) {
                const addInitAnswer = yield inquirer_1.default.prompt({
                    type: 'nextjs-checkbox',
                    name: 'addInitializers',
                    message: 'Would you like to include any add-on initializers?',
                    choices: addOnChoices.filter((choice) => {
                        return (choice.platform === PlatformCompatibility.Both ||
                            (answers.xmcloud && choice.platform === PlatformCompatibility.XMC) ||
                            (!answers.xmcloud && choice.platform === PlatformCompatibility.SXP));
                    }),
                });
                addInitializers.push(...addInitAnswer.addInitializers);
            }
            if (!addInitializers.includes('nextjs-styleguide') &&
                !args.templates.includes('nextjs-styleguide')) {
                const pkg = (0, common_1.openJsonFile)(pkgPath);
                pkg.scripts.bootstrap = pkg.scripts.bootstrap.replace(' && graphql-let', '');
                (0, common_1.writeJsonFile)(pkg, pkgPath);
            }
            const response = {
                nextSteps: [`* Connect to Sitecore with ${chalk_1.default.green('jss setup')} (optional)`],
                appName: answers.appName,
                initializers: addInitializers,
            };
            return response;
        });
    }
}
exports.default = NextjsInitializer;
