"use strict";
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());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupOptions = void 0;
const schematics_1 = require("@angular-devkit/schematics");
const inquirer_1 = require("inquirer");
const options_configuration_1 = require("./config/options.configuration");
const json_file_1 = require("./helpers/json-file");
const strings_1 = require("@angular-devkit/core/src/utils/strings");
///////////////////////////////////////////////////////////
//////////////////// SetUp Options /////////////////////////
///////////////////////////////////////////////////////////
const setupOptions = (host, _options, context) => __awaiter(void 0, void 0, void 0, function* () {
    // ============ Get Project, if more than one in workspace inquire with prompt  ========================
    // #region get project
    let workspaceConfig;
    workspaceConfig = new json_file_1.JSONFile(host, "angular.json");
    let project;
    if (!workspaceConfig) {
        throw new schematics_1.SchematicsException("Not an Angular CLI workspace");
    }
    const projects = workspaceConfig.get(["projects"]);
    if (projects == undefined ||
        Object.keys(workspaceConfig.get(["projects"])).length == 0) {
        throw new schematics_1.SchematicsException("Not Angualar Projects Available");
    }
    const project_keys = Object.keys(projects);
    if (project_keys.length == 1) {
        _options.projectFound = project_keys[0];
    }
    else {
        const questions = [
            {
                type: "list",
                name: "project",
                message: "In which Project would you like to add angular-web3?",
                choices: project_keys,
            },
        ];
        const answer = yield (0, inquirer_1.prompt)(questions);
        _options.projectFound = answer["project"];
    }
    // #endregion get project
    // ============ get the source folder Path  ========================
    // #region get source folder path
    project = projects[_options.projectFound];
    _options.sourceRoot = project.sourceRoot;
    // #endregion get source path
    _options.alreadyInstalled = false;
    if (host.exists("hardhat/hardhat.config.ts") == true) {
        _options.alreadyInstalled = true;
        _options.demoToInstall = false;
    }
    // ============ get Demo app/services ========================
    // #region get source folder path
    if (_options.test == false && _options.alreadyInstalled == false) {
        const questions = [
            {
                type: "confirm",
                name: "demo",
                message: "Do you want to Install a Demo App",
                default: false,
            },
        ];
        const answer = yield (0, inquirer_1.prompt)(questions);
        _options.demoToInstall = answer.demo;
    }
    // ============ If Demo APP========================
    if (_options.demoToInstall == true) {
        if (_options.test == false) {
            const questionsDemo = [
                {
                    type: "list",
                    name: "demoApp",
                    message: "Which demoApp do you want to install",
                    choices: [
                        "Hello World Contract",
                        "Debug Contract",
                        "Nft Contract",
                        "Super Fluid",
                        "Lens Protocol",
                        "The Graph",
                    ],
                    filter(value) {
                        let valueChanged = value.split(" ").join("");
                        let first_ele = valueChanged.substring(0, 1).toLowerCase();
                        let rest = valueChanged.substring(1, valueChanged.length);
                        return first_ele + rest;
                    },
                },
            ];
            const answerDemo = yield (0, inquirer_1.prompt)(questionsDemo);
            _options.dappDemo = answerDemo.demoApp;
            _options.contractName = _options.dappDemo;
        }
        _options.addOns = options_configuration_1.configuration_options.dappDemos[_options.dappDemo].addOns;
    }
    else {
        // ============ If Not demo APP========================
        if (_options.test == false) {
            const questionNameContract = [
                {
                    name: "contractName",
                    type: "input",
                    default: "defaultContract",
                    message: "Enter Contract Name",
                    validate(value) {
                        if (value.length) {
                            return true;
                        }
                        return "It cannot be empty. Please enter it correctly...";
                    },
                    filter(value) {
                        return (0, strings_1.capitalize)((0, strings_1.camelize)((value)));
                    }
                },
            ];
            _options.dappDemo = "minimalContract";
            const answerName = yield (0, inquirer_1.prompt)(questionNameContract);
            _options.contractName = answerName.contractName;
            //// Ui's framework to install
            const questionsUiFramework = [
                {
                    type: "list",
                    name: "uiFramework",
                    message: "Do you want to install a Ui Framework",
                    default: "noop",
                    choices: [
                        "noop",
                        "PrimeNG",
                    ],
                    filter(value) {
                        let valueChanged = value.split(" ").join("");
                        let first_ele = valueChanged.substring(0, 1).toLowerCase();
                        let rest = valueChanged.substring(1, valueChanged.length);
                        return first_ele + rest;
                    },
                },
            ];
            const answerUI = yield (0, inquirer_1.prompt)(questionsUiFramework);
            _options.uiFramework = answerUI.uiFramework;
            //// Services to install
            const questionsServices = [
                {
                    type: "checkbox",
                    name: "dappServices",
                    message: "Which services would you like to add to your project?",
                    choices: [
                        "Ipfs Service",
                        "Super FluidService",
                        "Lit Protocol Service",
                        "GraphQl Service",
                        "Subgraph",
                        "Graph Node",
                    ],
                    filter(value) {
                        return value.map((map) => {
                            map = map.split(" ").join("");
                            let first_ele = map.substring(0, 1).toLowerCase();
                            let rest = map.substring(1, map.length);
                            return first_ele + rest;
                        });
                    },
                },
            ];
            const answerNoDemo = yield (0, inquirer_1.prompt)(questionsServices);
            _options.addOns = answerNoDemo.dappServices;
        }
    }
    // #endregion get Demo App/services
    if (_options.skipInstall == undefined) {
        _options.skipInstall = false;
    }
    // ============ Manual Angular Material installation  ========================
    if (_options.demoToInstall == true) {
        const styles_path = [
            "projects",
            _options.projectFound,
            "architect",
            "build",
            "options",
            "styles",
        ];
        const styles = workspaceConfig.get(styles_path);
        workspaceConfig.modify(styles_path, [
            "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
        ].concat(styles));
    }
    // ============ if IPFS change custom webpack  ========================
    // if (_options.addOns.indexOf("ipfsService") !== -1) {
    //     workspaceConfig.modify(
    //         [
    //             "projects",
    //             _options.projectFound as string,
    //             "architect",
    //             "build",
    //             "builder",
    //         ],
    //         "@angular-builders/custom-webpack:browser"
    //     );
    //     workspaceConfig.modify(
    //         [
    //             "projects",
    //             _options.projectFound as string,
    //             "architect",
    //             "build",
    //             "options",
    //             "customWebpackConfig",
    //             "path",
    //         ],
    //         "./extra-webpack.config.js"
    //     );
    //     workspaceConfig.modify(
    //         [
    //             "projects",
    //             _options.projectFound as string,
    //             "architect",
    //             "serve",
    //             "builder",
    //         ],
    //         "@angular-builders/custom-webpack:dev-server"
    //     );
    // }
    // host.overwrite("angular.json", JSON.stringify(workspaceConfig));
    return host;
});
exports.setupOptions = setupOptions;
//# sourceMappingURL=0-setupOptions.js.map