var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { Util } from './util/util';
import { Storage } from './util/storage';
export class Config {
    constructor() {
        this._config = {};
        this._locked = ['_config', '_defaults'];
    }
    get(key) {
        return this._config[key];
    }
    set(key, value, castToType = false) {
        if (!this.islocked(key)) {
            if (castToType) {
                value = Util.castToType(value);
            }
            return this._config[key] = value;
        }
        else {
            console.error('Config: property "' + key + '" can not be changed.');
        }
    }
    add(object, extend = false, castToType = false) {
        const self = this;
        if (!extend) {
            Object.keys(object).forEach((key) => {
                self.set(key, object[key], castToType);
            });
        }
        else {
            this.extend(object);
        }
    }
    fetch(url, extend = false) {
        return __awaiter(this, void 0, void 0, function* () {
            const self = this;
            // if(self.get('api_headers')){
            //
            // }
            // let config =  await fetch(url, {headers: self.get('api_headers')})
            let config = yield fetch(url)
                .then(function (response) {
                return response.json();
            })
                .catch(function (error) {
                console.log('config fetch error:', error.message);
            });
            if (extend) {
                this.extend(config);
            }
            else {
                Object.keys(config).forEach((k, i) => {
                    self.set(k, config[k]);
                });
            }
            return config;
        });
    }
    extend(object, target = this._config) {
        Util.deepAssign(target, object);
    }
    all() {
        return this._config;
    }
    getLocalSettings(id) {
        if (window.localStorage && window.localStorage.getItem(id)) {
            this.add(Storage.restoreLocalModel(id));
        }
    }
    setLocalSettings(id, settings = this._config) {
        if (window.localStorage) {
            Storage.saveLocalModel(settings, id);
        }
    }
    setDefaults(configDefaults) {
        const self = this;
        self._defaults = configDefaults;
        Object.keys(configDefaults).forEach((k) => {
            self.set(k, Util.copy(configDefaults[k]));
        });
    }
    restoreDefaults() {
        const self = this;
        Object.keys(self._defaults).forEach((k) => {
            self.set(k, self._defaults[k]);
        });
    }
    islocked(key) {
        // todo:
        this._locked.forEach((k) => {
            if (k === key) {
                console.log('Config error: property "' + k + '" is protected');
                return true;
            }
        });
    }
    // todo
    lock(key) {
        // todo:
        return false;
        // this._locked.forEach((k) => {
        //
        // })
    }
    unLock(key) {
        // todo:
        // this._locked.forEach((k) => {
        //
        // })
    }
    initXdomain(object) {
        if (typeof window !== 'undefined' && window['xdomain']) {
            window['xdomain'].masters(object.masters);
            window['xdomain'].slaves(object.slaves);
        }
    }
    ;
}
//# sourceMappingURL=config.js.map