"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const path = require("path");
const fs_1 = require("fs");
const git_user_1 = require("./git-user");
const I18n_1 = require("../../modules/I18n");
const checkReservedComponents_1 = require("../../Api/checkReservedComponents");
const getInquirerListType_1 = require("../../bin/utils/getInquirerListType");
const metadata = require('read-metadata');
function getOptions(name, id, dir, data) {
    const opts = getMetadata(dir);
    const validateNameFn = (name) => validateProjectName(name) && checkReservedComponents_1.default(name, data);
    setDefault(opts, 'name', name, validateNameFn);
    const author = git_user_1.default();
    if (author) {
        setDefault(opts, 'author', author);
    }
    updatePromptsType(opts);
    updateListSelectionResultText(opts);
    fillPlaceholders(opts, data);
    return opts;
}
exports.default = getOptions;
function getMetadata(dir) {
    const json = path.join(dir, 'meta.json');
    const js = path.join(dir, 'meta.js');
    let opts = {};
    if (fs_1.existsSync(json)) {
        opts = metadata.sync(json);
    }
    else if (fs_1.existsSync(js)) {
        const req = require(path.resolve(js));
        if (req !== Object(req)) {
            throw new Error('meta.js needs to expose an object');
        }
        opts = req;
    }
    return opts;
}
function fillPlaceholders(opts, data) {
    for (const field of Object.values(opts.prompts)) {
        ['message', 'default'].forEach((key) => {
            if (field[key] && typeof field[key] === 'string') {
                for (const dataKey of Object.keys(data)) {
                    field[key] = field[key].replace(`{{${dataKey}}}`, data[dataKey]);
                }
            }
        });
    }
}
function setDefault(opts, key, val, validateFn) {
    if (opts.schema) {
        opts.prompts = opts.schema;
        delete opts.schema;
    }
    const prompts = opts.prompts || (opts.prompts = {});
    if (!prompts[key] || typeof prompts[key] !== 'object') {
        prompts[key] = {
            type: 'string',
            default: val,
            validate: validateFn,
        };
    }
    else {
        prompts[key]['default'] = val;
    }
}
function validateProjectName(name) {
    if (!/^([a-z]|[0-9])([a-z]|[0-9]|-|_)*$/.test(name)) {
        console.log(` ${I18n_1.default.error('NOT_VALID_PROJECT_NAME', name)}`);
        return false;
    }
    return true;
}
function updatePromptsType(opts) {
    Object.keys(opts.prompts).forEach((key) => {
        if (opts.prompts[key].type === 'rawlist') {
            opts.prompts[key].type = getInquirerListType_1.default();
        }
    });
}
function updateListSelectionResultText(opts) {
    Object.keys(opts.prompts).forEach((key) => {
        if (opts.prompts[key].choices && Array.isArray(opts.prompts[key].choices)) {
            opts.prompts[key].choices.forEach((choice) => {
                choice.name && (choice.value.toString = () => choice.name);
            });
        }
    });
}
//# sourceMappingURL=options.js.map