"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.ngAdd = void 0;
const schematics_1 = require("@angular-devkit/schematics");
const createFiles_1 = require("./createFiles");
const addAndInstallDependencies_1 = require("./addAndInstallDependencies");
const addScriptsToPackageJson_1 = require("./addScriptsToPackageJson");
const contract_config_1 = require("./config/contract.config");
const updateTsConfig_1 = require("./updateTsConfig");
const _0_setupOptions_1 = require("./0-setupOptions");
const addFonts_1 = require("./addFonts");
const updateAppHtml_1 = require("./updateAppHtml");
const updateIndexHtml_1 = require("./updateIndexHtml");
const updateAngularJson_1 = require("./updateAngularJson");
const updateStyles_1 = require("./updateStyles");
const misc_1 = require("./helpers/misc");
const strings_1 = require("@angular-devkit/core/src/utils/strings");
/** Adds a package to the package.json in the given host tree. */
const changeContractConfig = (host, _options) => {
    const contractConfig = contract_config_1.contract_config;
    if (_options.alreadyInstalled == false) {
        if (_options.contractName !== _options.dappDemo) {
            const jsonName = (0, misc_1.toUnder)(_options.contractName);
            let artifactsPath = `${(0, strings_1.capitalize)(_options.contractName)}.sol/${(0, strings_1.capitalize)(_options.contractName)}.json`;
            let obj = {
                "artifactsPath": artifactsPath,
                "name": (0, strings_1.capitalize)(_options.contractName),
                "ctor": [],
                "jsonName": (0, strings_1.dasherize)(_options.contractName)
            };
            const contractConfigString = JSON.stringify({
                [jsonName]: obj,
            });
            host.create("hardhat/contract.config.json", contractConfigString);
        }
        else {
            const contractConfigString = JSON.stringify({
                [_options.dappDemo]: contractConfig[_options.dappDemo],
            });
            host.create("hardhat/contract.config.json", contractConfigString);
        }
    }
    else {
        let alreadyConfig;
        alreadyConfig = JSON.parse(host.read("hardhat/contract.config.json").toString("utf-8"));
        let project;
        if (!alreadyConfig) {
            throw new schematics_1.SchematicsException("If project does exist, I should not be here");
        }
        alreadyConfig[_options.dappDemo] =
            contractConfig[_options.dappDemo];
        const contractConfigString = JSON.stringify(alreadyConfig);
        host.overwrite("hardhat/contract.config.json", contractConfigString);
    }
    return host;
};
const doTheLogs = (host, _options, context) => {
    context.logger.info("");
    context.logger.warn(`XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`);
    context.logger.warn(`Don't forget to add TO BLBLBLBL`);
    context.logger.warn(`XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`);
    context.logger.info("");
    return host;
};
function ngAdd(_options) {
    return (0, schematics_1.chain)([
        (tree, _context) => __awaiter(this, void 0, void 0, function* () {
            yield (0, _0_setupOptions_1.setupOptions)(tree, _options, _context);
        }),
        (tree, _context) => {
            changeContractConfig(tree, _options);
        },
        (tree, _context) => {
            return (0, createFiles_1.createFiles)(tree, _options, _context);
        },
        // (tree: Tree, _context: SchematicContext) => {
        //   _context.logger.warn(
        //     `101 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`
        //   );
        //   return addImport(tree, _options);
        // },
        (tree, _context) => {
            _context.logger.warn(`111 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`);
            return (0, updateAppHtml_1.updateAppHtml)(tree, _options);
        },
        (tree, _context) => {
            return (0, updateTsConfig_1.updateTsConfig)(tree, _options);
        },
        // (tree: Tree, _context: SchematicContext) => {
        //   return updatePolyfills(tree, _options);
        // },
        (tree, _context) => {
            return (0, updateAngularJson_1.updateAngularJson)(tree, _options);
        },
        (0, updateIndexHtml_1.updateIndexHtml)(_options),
        (tree, _context) => {
            return (0, updateStyles_1.updateStyles)(tree, _options);
        },
        (0, addScriptsToPackageJson_1.adScriptsToPackageJson)(_options),
        (0, addFonts_1.addFontsToIndex)(_options),
        // addNgrx(_options),
        // runExternal(_options),
        // externalSchematic("@angular/material", "ng-add", { project:_options.projectFound,
        //   animations: true, theme: "indigo-pink",  typography: false}),
        //externalSchematic("@ng-bootstrap/ng-bootstrap", "ng-add", { project:_options.projectFound}),
        (0, addAndInstallDependencies_1.addAndinstallDependencies)(_options),
        (tree, _context) => {
            return doTheLogs(tree, _options, _context);
        }
    ]);
}
exports.ngAdd = ngAdd;
//# sourceMappingURL=index.js.map