"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.manageConstObject = exports.parseNumber = exports.config = exports.getJSONFile = void 0;
const JSONC = require("jsonc-parser");
const terminal_1 = require("./terminal");
const utils_1 = require("./utils");
const jsonFiles = new Map();
const getJSONFile = (path) => {
    if (jsonFiles.has(path)) {
        return jsonFiles.get(path);
    }
    let f = terminal_1.getFile(path);
    if (f === null) {
        f = {
            mode: 0b110,
            data: '{}',
        };
        const dir = terminal_1.getDirOf(path);
        if (dir === null) {
            console.error(`Something terrible is wrong here. Parent directory (${dir}) not found.`);
            return null;
        }
        dir.contents.set(terminal_1.getFilename(path), f);
    }
    let settings = {};
    try {
        settings = JSONC.parse(f.data || '{}');
    }
    catch {
        console.log('Settings file was corrupted, stored as settings.corrupted');
        settings.corrupted = f.data;
    }
    settings.save = () => {
        terminal_1.writeFile(f, JSON.stringify(settings, null, '  '));
    };
    jsonFiles.set(path, settings);
    return settings;
};
exports.getJSONFile = getJSONFile;
exports.config = exports.getJSONFile('/home/asconfig');
const binObj = {
    config: {
        mode: 0b001,
        exec: (args, _stdin, stdout, stderr, _wd, _env) => {
            if (args.length === 1) {
                const props = Object.getOwnPropertyNames(exports.config);
                for (let i = 0; i < props.length; i++) {
                    const p = props[i];
                    if (p === 'save') {
                        continue;
                    }
                    stdout.write(`config.${p} = ${JSON.stringify(exports.config[p])}\n`);
                }
                return;
            }
            if (args[1][0] === '-') {
                if (args[1].length === 0) {
                    stderr.write('Invalid flag: -\n');
                    return;
                }
                if (args[1][1] === 'd' && args.length > 2) {
                    delete exports.config[args[2]];
                    exports.config.save();
                    return;
                }
                return;
            }
            if (args.length >= 3) {
                try {
                    exports.config[args[1]] = JSONC.parse(args[2]) || args[2];
                }
                catch (e) {
                    stderr.write(`Unable to interpret value: ${args[2]} - ${e}\n`);
                    return;
                }
                exports.config.save();
            }
            stdout.write(`config.${args[1]} = ${JSON.stringify(exports.config[args[1]])}\n`);
        },
    },
};
terminal_1.addDirToIndex('/', {
    contents: utils_1.map({
        bin: {
            contents: utils_1.map(binObj),
        },
    }),
});
const parseNumber = (s) => {
    if (s.length > 2) {
        switch (s.substr(0, 2)) {
            case '0x':
                return parseInt(s.substr(2), 16);
            case '0b':
                return parseInt(s.substr(2), 2);
            default:
                return parseFloat(s);
        }
    }
    return s.length === 0 ? 0 : parseFloat(s);
};
exports.parseNumber = parseNumber;
const manageConstObject = (obj, objName) => (args, _stdin, stdout, _stderr, _wd, _env) => {
    if (args.length === 1) {
        for (const key in obj) {
            if (utils_1.objHas(obj, key)
                && typeof obj[key] !== 'function'
                && typeof obj[key] !== 'object') {
                stdout.write(`${objName}.${key}: ${typeof obj[key]} = ${obj[key]}\n`);
            }
        }
        return 0;
    }
    if (args[1] === 'get') {
        stdout.write(`${objName}.${args[2]} = ${obj[args[2]]}\n`);
        return 0;
    }
    switch (typeof obj[args[1]]) {
        case 'string':
            obj[args[1]] = args.length > 2 ? args[2] : '';
            break;
        case 'number':
            if (args.length > 2) {
                obj[args[1]] = exports.parseNumber(args[2]);
            }
            else {
                obj[args[1]] = obj[args[1]] === 0 ? 1 : 0;
            }
            break;
        case 'boolean':
            obj[args[1]] = args.length > 2 ? utils_1.ok.has(args[2]) : !obj[args[1]];
            break;
        case 'undefined':
            stdout.write(`Unknown ${objName} param: ${args[1]}\n`);
            return 1;
        default:
            stdout.write(`Cannot set type of ${objName} param: ${args[1]}\n`);
            return 1;
    }
    stdout.write(`Set ${objName} param ${args[1]} to ${obj[args[1]]}\n`);
    return 0;
};
exports.manageConstObject = manageConstObject;
//# sourceMappingURL=fsutils.js.map