"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.safePath = safePath;
exports.prependPath = prependPath;
exports.shiftPath = shiftPath;
exports.hasKey = hasKey;
exports.keys = keys;
exports.read = read;
exports.get = get;
exports.set = set;
exports.delete_ = delete_;
exports.write = write;
const f_1 = require("../../system/f/object/Keys/f");
function safePath(path) {
    return `${path}/`;
}
function prependPath(path, key) {
    return `${safePath(path)}${key}`;
}
function shiftPath(path, key) {
    return key.slice(path.length + 1);
}
function hasKey(storage, path, key) {
    const key_ = prependPath(path, key);
    const item = storage.getItem(key_);
    const has = !!item;
    return has;
}
function keys(storage, path) {
    const path_ = safePath(path);
    const allKeys = (0, f_1.keys)(storage);
    const keys = [];
    for (const key of allKeys) {
        if (key.startsWith(path_)) {
            const relativeKey = shiftPath(path, key);
            keys.push(relativeKey);
        }
    }
    return keys;
}
function read(storage, path) {
    const obj = {};
    const keys_ = keys(storage, path);
    for (const key of keys_) {
        const _key = shiftPath(path, key);
        obj[_key] = storage.getItem(key);
    }
    return obj;
}
function get(storage, path, name) {
    const key = prependPath(path, name);
    const value = storage.getItem(key);
    if (value === null) {
        throw new Error('key not found');
    }
    else {
        return value;
    }
}
function set(storage, path, name, value) {
    const key = prependPath(path, name);
    storage.setItem(key, value);
}
function delete_(storage, path, name) {
    const key = prependPath(path, name);
    storage.removeItem(key);
}
function write(storage, path, obj) {
    const keys_ = keys(storage, path);
    for (const key of keys_) {
        storage.removeItem(key);
    }
    for (const key in obj) {
        set(storage, path, key, obj[key]);
    }
}
//# sourceMappingURL=storage.js.map