"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NextjsCheckbox = exports.prompts = exports.sharedPrerender = exports.Prerender = void 0;
const checkbox_1 = __importDefault(require("inquirer/lib/prompts/checkbox"));
const common_1 = require("../../common");
var Prerender;
(function (Prerender) {
    Prerender["SSG"] = "SSG";
    Prerender["SSR"] = "SSR";
})(Prerender || (exports.Prerender = Prerender = {}));
const DEFAULT_PRERENDER = Prerender.SSG;
/*
 * Shared prerender configuration for the selected prerendering strategy (SSG or SSR).
 *
 * This object is initialized with a default prerender value (`SSG`) and is updated
 * dynamically after the primary Next.js initializer's initialization process based on
 * user input collected via prompts without having to re-prompt or duplicate logic.
 */
exports.sharedPrerender = {
    prerender: DEFAULT_PRERENDER,
};
// still need sxp prompts here until sitecore/config is no longer added to xmc app
exports.prompts = [
    ...common_1.clientAppPrompts,
    ...common_1.sxpPrompts,
    {
        type: 'list',
        name: 'prerender',
        message: 'How would you like to prerender your application?',
        choices: Object.values(Prerender),
        default: DEFAULT_PRERENDER,
        when: (answers) => {
            if (answers.yes && !answers.prerender) {
                answers.prerender = DEFAULT_PRERENDER;
            }
            return !answers.prerender;
        },
    },
    {
        type: 'confirm',
        name: 'xmcloud',
        message: 'Are you building for Sitecore XM Cloud?',
        default: false,
        when: (answers) => {
            // don't prompt if --yes or nextjs-xmcloud template was specified
            if (answers.yes) {
                return false;
            }
            else if (answers.templates.includes('nextjs-xmcloud')) {
                answers.xmcloud = true;
                return false;
            }
            return true;
        },
    },
];
/**
 * Custom `inquirer` control to support error messages
 */
class NextjsCheckbox extends checkbox_1.default {
    onSpaceKey() {
        super.onSpaceKey();
        const isSelected = (initializer) => this.opt.choices.choices.find((ch) => {
            const { value, checked } = ch;
            return value === initializer && checked;
        });
        const isSxaSelected = isSelected('nextjs-sxa');
        const isStyleguideSelected = isSelected('nextjs-styleguide');
        if (isSxaSelected && isStyleguideSelected) {
            this.onError({
                isValid: (0, common_1.incompatibleAddonsMsg)('nextjs-sxa', 'nextjs-styleguide'),
            });
        }
    }
}
exports.NextjsCheckbox = NextjsCheckbox;
