"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createFiles = void 0;
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const strings_1 = require("@angular-devkit/core/src/utils/strings");
const getOptionsKeys_1 = require("./helpers/getOptionsKeys");
const contract_config_1 = require("./config/contract.config");
const stringUtils = { classify: strings_1.classify, dasherize: strings_1.dasherize, camelize: strings_1.camelize, underscore: strings_1.underscore, capitalize: strings_1.capitalize };
const createFiles = (host, _options, context) => {
    const demoName = contract_config_1.contract_config[_options.dappDemo].name;
    const contractName = _options.contractName == undefined ? demoName : _options.contractName;
    _options.contractName = contractName;
    let toInstallKeys = (0, getOptionsKeys_1.getOptionskeys)(_options, context);
    const templateRules = [];
    let templates_src = [];
    let templates_root = [];
    toInstallKeys.forEach(ele => {
        templates_root = templates_root.concat(ele.templates_root);
        templates_src = templates_src.concat(ele.templates_src);
    });
    /// ============ components common for demo apps ================= 
    // if (_options.demoToInstall== true){
    //     templates_src.push(     {
    //       source: "./files/demos/components",
    //       target: "/app/dapp-components",
    //     },)
    // }
    /// ============  demo apps ================= 
    const options_file_replacements = Object.assign(Object.assign({}, stringUtils), { sourceRoot: _options.sourceRoot, contractCode: _options.dappDemo, contractName, metadata: _options.dappDemo + 'Metadata', jsonFile: (0, strings_1.underscore)((0, strings_1.dasherize)(_options.dappDemo)), demoName });
    for (const rootFile of templates_root) {
        const templateRoot = (0, schematics_1.apply)((0, schematics_1.url)(rootFile.source), [
            (0, schematics_1.applyTemplates)(options_file_replacements),
            (0, schematics_1.move)((0, core_1.normalize)(rootFile.target)),
        ]);
        templateRules.push((0, schematics_1.mergeWith)(templateRoot, schematics_1.MergeStrategy.Overwrite));
    }
    for (const srcFile of templates_src) {
        const templateSrc = (0, schematics_1.apply)((0, schematics_1.url)(srcFile.source), [
            (0, schematics_1.applyTemplates)(options_file_replacements),
            (0, schematics_1.move)((0, core_1.normalize)(`/${_options.sourceRoot}${srcFile.target}`)),
        ]);
        templateRules.push((0, schematics_1.mergeWith)(templateSrc, schematics_1.MergeStrategy.AllowCreationConflict));
    }
    const templateCommonHardhat = (0, schematics_1.apply)((0, schematics_1.url)("./files/common/hardhat"), [
        (0, schematics_1.applyTemplates)({ sourceRoot: _options.sourceRoot, contractName, contractCode: _options.dappDemo }),
        (0, schematics_1.move)((0, core_1.normalize)(`/hardhat/`)),
    ]);
    // templateRules.push(      mergeWith(templateCommonHardhat, MergeStrategy.Overwrite)    );
    const templateInjector = (0, schematics_1.apply)((0, schematics_1.url)("./files/common/dapp/dapp-injector"), [
        (0, schematics_1.applyTemplates)({
            sourceRoot: _options.sourceRoot,
            metadata: _options.dappDemo + 'Metadata',
            demoName, contractName
        }),
        (0, schematics_1.move)((0, core_1.normalize)((0, core_1.normalize)(`/${_options.sourceRoot}/app/dapp-injector`))),
    ]);
    //templateRules.push(mergeWith(templateInjector, MergeStrategy.Overwrite));
    const templateCommonApp = (0, schematics_1.apply)((0, schematics_1.url)("./files/common/dapp/app"), [
        (0, schematics_1.applyTemplates)(Object.assign(Object.assign({}, stringUtils), { contractCode: _options.dappDemo, contractName })),
        (0, schematics_1.move)((0, core_1.normalize)((0, core_1.normalize)(`/${_options.sourceRoot}/app/`))),
    ]);
    //templateRules.push(mergeWith(templateCommonApp, MergeStrategy.Overwrite));
    return (0, schematics_1.chain)(templateRules);
};
exports.createFiles = createFiles;
//# sourceMappingURL=createFiles.js.map