"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.writeSystemSettings = void 0;
const alias_resolver_1 = require("@ewizardjs/alias-resolver");
const system_settings_1 = require("@ewizardjs/system-settings");
const fs_extra_1 = require("fs-extra");
const ramda_1 = require("ramda");
const upath_1 = require("@ewizardjs/upath");
function writeSystemSettings(dest, repoName, masterTemplateLayout, systemSettings) {
    return __awaiter(this, void 0, void 0, function* () {
        const ewizardDir = (0, upath_1.join)(dest, '.ewizard');
        const systemSettingsDest = (0, upath_1.join)(ewizardDir, 'settings.json');
        const path = yield getSystemSettingsPaths(systemSettings.path, repoName, masterTemplateLayout);
        yield (0, fs_extra_1.ensureDir)(ewizardDir);
        return (0, fs_extra_1.writeJSON)(systemSettingsDest, (0, ramda_1.mergeRight)(systemSettings, { path }), {
            spaces: 2,
        });
    });
}
exports.writeSystemSettings = writeSystemSettings;
function getSystemSettingsPaths(path, repoName, masterTemplateLayout) {
    return __awaiter(this, void 0, void 0, function* () {
        const aliasedPaths = (yield (0, alias_resolver_1.traverseAsync)(path, traverseCb));
        const masterTemplatePath = (0, upath_1.join)('node_modules', repoName);
        aliasedPaths.masterTemplatePath = masterTemplatePath;
        if (masterTemplateLayout) {
            aliasedPaths.masterTemplateLayout = masterTemplateLayout;
        }
        return aliasedPaths;
    });
}
function traverseCb(context) {
    return __awaiter(this, void 0, void 0, function* () {
        if (!isTraversalValueString(context)) {
            return;
        }
        const { parent, key, value, keyPath } = context;
        parent[key] = isPathIgnored(keyPath, value)
            ? (0, upath_1.toUnix)(value)
            : (0, upath_1.join)(alias_resolver_1.ALIAS, value);
    });
}
function isTraversalValueString({ parent, key, value }) {
    return parent && key !== null && typeof value === 'string';
}
function isPathIgnored(keyPath, value) {
    const ignoreKeyPaths = (0, system_settings_1.getAliasIgnoredKeypaths)();
    return ignoreKeyPaths.some((item) => keyPath.includes(item)) || value.startsWith(alias_resolver_1.ALIAS);
}
//# sourceMappingURL=writeSystemSettings.js.map