"use strict";
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
exports.__esModule = true;
exports.globalStorage = exports.instanceStorage = void 0;
var dotenv_1 = __importDefault(require("dotenv"));
var fs_1 = __importDefault(require("fs"));
var os_1 = __importDefault(require("os"));
var path_1 = __importDefault(require("path"));
var fs_extra_1 = __importDefault(require("fs-extra"));
var instanceStorage = (function () {
    function instanceStorage(path) {
        fs_extra_1["default"].ensureFileSync(path);
        this._storageFilePath = path;
        instanceStorage._storageFilePathStatic = path;
        this._storageJsonData = dotenv_1["default"].parse(fs_1["default"].readFileSync(path));
    }
    instanceStorage.initInstance = function () {
        if (!instanceStorage.instance) {
            instanceStorage.instance = new instanceStorage(path_1["default"].resolve(os_1["default"].homedir(), ".zepp/.zeus"));
        }
        return instanceStorage.instance;
    };
    instanceStorage.prototype._syncStorageEnvToJson = function () {
        this._storageJsonData = dotenv_1["default"].parse(fs_1["default"].readFileSync(this._storageFilePath));
    };
    instanceStorage.prototype._syncJsonDataToStorageEnv = function () {
        var _this = this;
        var dotenvArray = [];
        Object.keys(this._storageJsonData).forEach(function (key) {
            dotenvArray.push("".concat(key, "=").concat(_this._storageJsonData[key]));
        });
        fs_1["default"].writeFileSync(this._storageFilePath, dotenvArray.join('\n'));
    };
    instanceStorage.getStorage = function (key) {
        var result = dotenv_1["default"].parse(fs_1["default"].readFileSync(instanceStorage._storageFilePathStatic));
        if (!key)
            return result;
        Object.keys(result).forEach(function (itemK) {
            if (!itemK.includes(key)) {
                delete result[itemK];
            }
        });
        return result;
    };
    instanceStorage.prototype.get = function (key) {
        return this._storageJsonData[key] || '';
    };
    instanceStorage.prototype.has = function (key) {
        return !!this._storageJsonData[key];
    };
    instanceStorage.prototype.set = function (obj) {
        this._storageJsonData = __assign(__assign({}, this._storageJsonData), obj);
        this._syncJsonDataToStorageEnv();
    };
    instanceStorage.prototype.deleteStorage = function (key) {
        var _this = this;
        var getLocalConfig = require('../config/project').getLocalConfig;
        var loginStorageKey = getLocalConfig().loginStorageKey;
        if (!key) {
            this._storageJsonData = {};
        }
        else if (key === '____login') {
            var ____loginKey_1 = Object.values(loginStorageKey).join(',');
            Object.keys(this._storageJsonData).forEach(function (itemK) {
                if (____loginKey_1.includes(itemK)) {
                    delete _this._storageJsonData[itemK];
                }
            });
        }
        else {
            Object.keys(this._storageJsonData).forEach(function (itemK) {
                if (itemK.includes(key)) {
                    delete _this._storageJsonData[itemK];
                }
            });
        }
        this._syncJsonDataToStorageEnv();
    };
    return instanceStorage;
}());
exports.instanceStorage = instanceStorage;
exports.globalStorage = instanceStorage.initInstance();
