"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    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) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const Unit_1 = require("../../../../../Class/Unit");
const EventEmitter_1 = require("../../../../../EventEmitter");
const storage_1 = require("../../../../../client/util/web/storage");
const APINotImplementedError_1 = require("../../../../../exception/APINotImplementedError");
const MethodNotImplementedError_1 = require("../../../../../exception/MethodNotImplementedError");
const _ids_1 = require("../../../../_ids");
class _LocalStorage extends Unit_1.Unit {
    constructor(system) {
        super({
            i: [],
            o: [],
        }, {}, system, _ids_1.ID_LOCAL_STORAGE);
        this.___emitter = new EventEmitter_1.EventEmitter_();
        this._checkAPI = () => {
            if (!localStorage) {
                throw new APINotImplementedError_1.APINotSupportedError('Local Storage');
            }
        };
    }
    subscribe(path, key, listener) {
        const { emitter } = this.__system;
        if (path.length > 0) {
            throw new Error('local storage path length must be 0');
        }
        const setListener = (key_, data) => {
            if (key === '*' || key_ === key) {
                listener('set', path, key_, data);
            }
        };
        const deleteListener = (data) => {
            listener('delete', path, key, data);
        };
        emitter.addListener('set', setListener);
        emitter.addListener('delete', deleteListener);
        const unlisten = () => {
            emitter.removeListener('set', setListener);
            emitter.removeListener('delete', deleteListener);
        };
        return unlisten;
    }
    read() {
        return __awaiter(this, void 0, void 0, function* () {
            // TODO system
            const obj = (0, storage_1.getAllLocalStorage)();
            return obj;
        });
    }
    write(data) {
        return __awaiter(this, void 0, void 0, function* () {
            // TODO
            return;
        });
    }
    get(name) {
        return __awaiter(this, void 0, void 0, function* () {
            this._checkAPI();
            const value = localStorage.getItem(name);
            if (value === null) {
                throw new Error('item not found');
            }
            else {
                return value;
            }
        });
    }
    set(name, data) {
        return __awaiter(this, void 0, void 0, function* () {
            const { emitter } = this.__system;
            this._checkAPI();
            localStorage.setItem(name, data);
            emitter.emit('set', name, data);
        });
    }
    delete(name) {
        return __awaiter(this, void 0, void 0, function* () {
            const { emitter } = this.__system;
            this._checkAPI();
            const data = localStorage.getItem(name);
            localStorage.removeItem(name);
            emitter.emit('delete', name, data);
        });
    }
    deepSet(path, data) {
        return __awaiter(this, void 0, void 0, function* () {
            throw new MethodNotImplementedError_1.MethodNotImplementedError();
        });
    }
    deepGet(path) {
        return __awaiter(this, void 0, void 0, function* () {
            throw new MethodNotImplementedError_1.MethodNotImplementedError();
        });
    }
    deepDelete(path) {
        return __awaiter(this, void 0, void 0, function* () {
            throw new MethodNotImplementedError_1.MethodNotImplementedError();
        });
    }
    keys() {
        return __awaiter(this, void 0, void 0, function* () {
            const keys = (0, storage_1.getStorageKeys)(localStorage);
            return keys;
        });
    }
    hasKey(name) {
        return __awaiter(this, void 0, void 0, function* () {
            const has = (0, storage_1.storageHasKey)(localStorage, name);
            return has;
        });
    }
}
exports.default = _LocalStorage;
//# sourceMappingURL=index.js.map