"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ProjectService = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
class ProjectService {
    constructor(path) {
        this.path = path;
    }
    readFile(path) {
        return new Promise((resolve, reject) => {
            fs_extra_1.default.readFile(path, { encoding: 'utf-8' }, (err, data) => {
                if (err) {
                    reject(err);
                }
                else {
                    resolve(data.toString());
                }
            });
        });
    }
    getServiceDefs() {
        return Promise.resolve('{}');
    }
    getAppJs() {
        return this.readFile(this.path + '/app.js');
    }
    getAppCss() {
        return this.readFile(this.path + '/app.css');
    }
    getAppVariables() {
        return this.readFile(this.path + '/app.variables.json')
            .then(data => JSON.parse(data));
    }
    getAppThemeVariables() {
        return this.readFile(this.path + '/app.theme.variables.js');
    }
    getFormatters() {
        return this.readFile(this.path + '/extensions/formatters.js');
    }
    getAppJSON() {
        return this.readFile(this.path + '/app.json')
            .catch(() => { })
            .then(data => JSON.parse(data || '{}'));
    }
    getRNConfig() {
        return this.readFile(this.path + '/wm_rn_config.json');
    }
    getPageConfigs() {
        return this.readFile(this.path + '/pages/pages-config.json')
            .then(data => JSON.parse(data));
    }
    getPageInfo(pageName) {
        return Promise.resolve({
            markup: fs_extra_1.default.readFileSync(this.path + `/pages/${pageName}/${pageName}.html`, 'utf-8'),
            script: fs_extra_1.default.readFileSync(this.path + `/pages/${pageName}/${pageName}.js`, 'utf-8'),
            styles: fs_extra_1.default.readFileSync(this.path + `/pages/${pageName}/${pageName}.css`, 'utf-8'),
            variables: fs_extra_1.default.readFileSync(this.path + `/pages/${pageName}/${pageName}.variables.json`, 'utf-8')
        });
    }
    getPrefabs() {
        let prefabs = [];
        const prefabDir = `${this.path}/WEB-INF/prefabs/`;
        if (fs_extra_1.default.existsSync(prefabDir)) {
            prefabs = fs_extra_1.default.readdirSync(prefabDir, {
                withFileTypes: true
            }).filter(f => f && f.isDirectory())
                .map(f => f.name);
        }
        return Promise.all(prefabs);
    }
    getPrefabInfo(prefabName) {
        const path = this.path.split('/WEB-INF')[0];
        return Promise.resolve({
            config: JSON.parse(fs_extra_1.default.readFileSync(`${path}/WEB-INF/prefabs/${prefabName}/webapp/config.json`, 'utf-8') || '{}')
        });
    }
    getExpoPlugins() {
        if (fs_extra_1.default.existsSync(`${this.path}/expo-plugins`)) {
            return Promise.resolve(this.path + '/expo-plugins');
        }
        return Promise.resolve('');
    }
    getResources() {
        return Promise.resolve(this.path + '/resources');
    }
    getFontConfig() {
        return Promise.resolve().then(() => fs_extra_1.default.readFileSync(this.path + `/font.config.js`, 'utf-8'));
    }
    getThemes() {
        return Promise.resolve([...this.getAllThemes(), ...this.getAllThemes('extraThemes')]);
    }
    getAllThemes(s = 'themes') {
        const themeDir = this.path + '/' + s;
        if (fs_extra_1.default.existsSync(themeDir)) {
            const themes = fs_extra_1.default.readdirSync(themeDir, {
                withFileTypes: true
            }).filter(f => f && f.isDirectory())
                .map(f => this.path + '/' + s + '/' + f.name);
            return themes;
        }
        return [];
    }
    geti18NFiles() {
        return Promise.resolve(`${this.path}/../../../i18n`);
    }
    getPrefabProjectService(prefabName) {
        return new ProjectService(`${this.path.split('/WEB-INF')[0]}/WEB-INF/prefabs/${prefabName}/webapp`);
    }
    getPackageJSONOverride() {
        if (fs_extra_1.default.existsSync(`${this.path}/package.json`)) {
            return fs_extra_1.default.readJSONSync(`${this.path}/package.json`, { encoding: 'utf-8' });
        }
        return Promise.resolve({});
    }
    copyNpmPackages(dest) {
        if (fs_extra_1.default.existsSync(`${this.path}/npm-packages`)) {
            fs_extra_1.default.copySync(`${this.path}/npm-packages`, `${dest}/npm-packages`);
        }
        return Promise.resolve();
    }
    getSelectedTheme() {
        if (fs_extra_1.default.existsSync(`${this.path}/themes/themes-config.json`)) {
            const result = fs_extra_1.default.readJSONSync(`${this.path}/themes/themes-config.json`, { encoding: 'utf-8' });
            return result === null || result === void 0 ? void 0 : result.activeTheme;
        }
    }
    async getWMXConfig(name) {
        const wmxConfig = `${this.path}/extensions/components/src/${name}/wmx.json`;
        if (fs_extra_1.default.existsSync(wmxConfig)) {
            return JSON.parse(fs_extra_1.default.readFileSync(wmxConfig, 'utf-8') || '{}');
        }
        return {};
    }
    getWMXComponents() {
        const componentsDir = `${this.path}/extensions/components/src`;
        if (fs_extra_1.default.existsSync(componentsDir)) {
            const components = fs_extra_1.default.readdirSync(componentsDir);
            return components.filter((component) => {
                return fs_extra_1.default.existsSync(`${componentsDir}/${component}/wmx.json`);
            });
        }
        return [];
    }
}
exports.ProjectService = ProjectService;
let instance;
exports.default = (config) => {
    if (!instance && config) {
        instance = new ProjectService(config.src);
    }
    return instance;
};
//# sourceMappingURL=project.service.js.map