"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isValidLocale = exports.validMomentLocales = exports.readAndReplaceFileContent = exports.findNodeModules = exports.readdir = exports.writeFile = exports.loadTemplate = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const handlebars_1 = __importDefault(require("handlebars"));
const profile_1 = __importDefault(require("./profiles/profile"));
const loadTemplate = (templatePath) => {
    const template = fs_extra_1.default.readFileSync(templatePath, 'utf8');
    const fn = handlebars_1.default.compile(template);
    return (data) => fn({ profile: profile_1.default, ...data });
};
exports.loadTemplate = loadTemplate;
const writeFile = (path, content) => {
    const parent = path.substring(0, path.lastIndexOf('/'));
    if (!fs_extra_1.default.existsSync(parent)) {
        fs_extra_1.default.mkdirpSync(parent);
    }
    if (profile_1.default.targetPlatform === 'native' && (path.endsWith('js') || path.endsWith('jsx'))) {
        content = content.replace(/debugger/ig, '//debugger');
    }
    fs_extra_1.default.writeFileSync(path, content);
};
exports.writeFile = writeFile;
const readdir = (path, opts = { recursive: false }, callback = (f) => ({})) => {
    fs_extra_1.default.readdirSync(path).forEach(name => {
        const child = `${path}/${name}`;
        if (fs_extra_1.default.statSync(child).isDirectory() && opts.recursive) {
            (0, exports.readdir)(child, opts, callback);
        }
        else {
            callback(child);
        }
    });
};
exports.readdir = readdir;
const findNodeModules = (path) => {
    if (fs_extra_1.default.existsSync(`${path}/node_modules`)) {
        return `${path}/node_modules`;
    }
    else {
        const splits = path.split('/');
        splits.pop();
        if (splits.length) {
            return (0, exports.findNodeModules)(splits.join('/'));
        }
    }
    return '';
};
exports.findNodeModules = findNodeModules;
const readAndReplaceFileContent = async function (path, writeFn) {
    if (!fs_extra_1.default.existsSync(path)) {
        return Promise.resolve();
    }
    const content = fs_extra_1.default.readFileSync(path, 'utf-8');
    return Promise.resolve().then(() => {
        return writeFn && writeFn(content);
    }).then((modifiedContent) => {
        if (modifiedContent !== undefined && modifiedContent !== null) {
            fs_extra_1.default.writeFileSync(path, modifiedContent);
            return modifiedContent;
        }
        return content;
    });
};
exports.readAndReplaceFileContent = readAndReplaceFileContent;
// * node_modules/moment/locale
exports.validMomentLocales = [
    "af", "ar-dz", "ar-kw", "ar-ly", "ar-ma", "ar-ps", "ar-sa", "ar-tn", "ar",
    "az", "be", "bg", "bm", "bn-bd", "bn", "bo", "br", "bs", "ca", "cs", "cv",
    "cy", "da", "de-at", "de-ch", "de", "dv", "el", "en-au", "en-ca", "en-gb",
    "en-ie", "en-il", "en-in", "en-nz", "en-sg", "eo", "es-do", "es-mx", "es-us",
    "es", "et", "eu", "fa", "fi", "fil", "fo", "fr-ca", "fr-ch", "fr", "fy",
    "ga", "gd", "gl", "gom-deva", "gom-latn", "gu", "he", "hi", "hr", "hu", "hy-am",
    "id", "is", "it-ch", "it", "ja", "jv", "ka", "kk", "km", "kn", "ko", "ku-kmr",
    "ku", "ky", "lb", "lo", "lt", "lv", "me", "mi", "mk", "ml", "mn", "mr", "ms-my",
    "ms", "mt", "my", "nb", "ne", "nl-be", "nl", "nn", "oc-lnc", "pa-in", "pl", "pt-br",
    "pt", "ro", "ru", "sd", "se", "si", "sk", "sl", "sq", "sr-cyrl", "sr", "ss", "sv",
    "sw", "ta", "te", "tet", "tg", "th", "tl-ph", "tlh", "tr", "tzl", "tzm-latn", "tzm",
    "ug-cn", "uk", "ur", "uz-latn", "uz", "vi", "x-pseudo", "yo", "zh-cn", "zh-hk",
    "zh-mo", "zh-tw"
];
const isValidLocale = (locale) => {
    try {
        return exports.validMomentLocales.includes(locale);
    }
    catch (error) {
        return false;
    }
};
exports.isValidLocale = isValidLocale;
//# sourceMappingURL=utils.js.map