import * as TypeChecks from "./common/type-checks.js";
// ✨
export class Session {
    constructor(props) {
        this.crypto = props.crypto;
        this.fs = props.fs;
        this.storage = props.storage;
        this.type = props.type;
        this.username = props.username;
    }
    async destroy() {
        await this.storage.removeItem(this.storage.KEYS.ACCOUNT_UCAN);
        await this.storage.removeItem(this.storage.KEYS.CID_LOG);
        await this.storage.removeItem(this.storage.KEYS.SESSION);
        await this.storage.removeItem(this.storage.KEYS.UCANS);
        await this.crypto.keystore.clearStore();
        if (this.fs)
            this.fs.deactivate();
    }
}
export function isSessionInfo(a) {
    return TypeChecks.isObject(a)
        && TypeChecks.hasProp(a, "username")
        && TypeChecks.hasProp(a, "type");
}
/**
 * Begin to restore a `Session` by looking up the `SessionInfo` in the storage.
 */
export async function restore(storage) {
    return storage
        .getItem(storage.KEYS.SESSION)
        .then((a) => a ? a : null)
        .then(a => a ? JSON.parse(a) : null)
        .then(a => isSessionInfo(a) ? a : null);
}
/**
 * Prepare the system for the creation of a `Session`
 * by adding the necessary info to the storage.
 */
export function provide(storage, info) {
    return storage.setItem(storage.KEYS.SESSION, JSON.stringify({ type: info.type, username: info.username }));
}
//# sourceMappingURL=session.js.map