"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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __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 path_1 = __importStar(require("path"));
const common_1 = require("../../common");
const proxy_1 = require("../../common/prompts/proxy");
const inquirer_1 = __importDefault(require("inquirer"));
const helpers_1 = require("../../common/utils/helpers");
class AngularXmCloudInitializer {
    get isBase() {
        return false;
    }
    init(args) {
        return __awaiter(this, void 0, void 0, function* () {
            var _a, _b, _c, _d;
            const pkg = (0, common_1.openJsonFile)(`${args.destination}${path_1.sep}package.json`);
            const addInitializers = [];
            // angular-xmcloud requires node-xmcloud-proxy
            if (!args.templates.includes('node-xmcloud-proxy')) {
                addInitializers.push('node-xmcloud-proxy');
            }
            // ensure args.proxyAppDestination is populated
            const promptArgs = {
                yes: args.yes,
                destination: args.destination,
                proxyName: 'node-xmcloud-proxy',
                proxyAppDestination: args.proxyAppDestination,
            };
            const proxyDetails = yield inquirer_1.default.prompt(proxy_1.proxyPrompts, promptArgs);
            args.proxyAppDestination = proxyDetails.proxyAppDestination;
            const finalArgs = Object.assign(Object.assign(Object.assign(Object.assign({}, args), { appName: args.appName || ((_a = pkg === null || pkg === void 0 ? void 0 : pkg.config) === null || _a === void 0 ? void 0 : _a.appName) || common_1.DEFAULT_APPNAME, appPrefix: args.appPrefix || ((_b = pkg === null || pkg === void 0 ? void 0 : pkg.config) === null || _b === void 0 ? void 0 : _b.prefix) || false }), proxyDetails), { relativeProxyAppDestination: (0, helpers_1.getRelativeProxyDestination)(args.destination, args.proxyAppDestination) });
            const templatePath = path_1.default.resolve(__dirname, '../../templates/angular-xmcloud');
            yield (0, common_1.transform)(templatePath, finalArgs);
            if (args.templates.includes('angular-sxp') || ((_d = (_c = pkg.config) === null || _c === void 0 ? void 0 : _c.templates) === null || _d === void 0 ? void 0 : _d.includes('angular-sxp'))) {
                console.log((0, common_1.incompatibleAddonsMsg)('angular-xmcloud', 'angular-sxp'));
            }
            const response = {
                nextSteps: [],
                appName: args.appName || common_1.DEFAULT_APPNAME,
                initializers: addInitializers,
            };
            return response;
        });
    }
}
exports.default = AngularXmCloudInitializer;
