
$(() => {
    class AccessManager {
        #linkStoredAccesses = new Set();

        constructor() {
            this.#linkStoredAccesses.clear();
            let linkStoredAccesses = [];
            try {
                linkStoredAccesses = (JSON.parse(localStorage?.getItem("accessLinks") || "[]") || []);
            } catch (err) {
                console.error(err);
            }
            linkStoredAccesses.forEach(i => this.#linkStoredAccesses.add(i));
        }

        #UpdateStorage() {
            localStorage?.setItem("accessLinks", JSON.stringify(Array.from(this.#linkStoredAccesses)));
        }

        Logout(code) {
            let value = null;
            this.#linkStoredAccesses.forEach(i => {
                if (i.key === code) {
                    this.#linkStoredAccesses.delete(i);
                    value = i.value;
                }
            });
            if (!value)
                return;
            this.#UpdateStorage();
            return LoadingTasks.push(() => {
                return new Promise(ok => {
                    $.ajax({
                        url: `/api/access/${code}`,
                        type: "DELETE",
                        success: data => {
                            window.ReloadAccessList(data);
                            MediaStorage.Instance.rebuildMetaList();
                            ok();
                        },
                        error: err => ok(false),
                    });
                });
            });
        }

        LinkLogin(linkList) {
            return new Promise(ok => {
                $.ajax({
                    url: "/api/access/link",
                    type: "POST",
                    data: { linkIds: JSON.stringify(Array.from(linkList)) },
                    success: data => {
                        for (let i in data)
                            this.#linkStoredAccesses.add({key: i, value: data[i].linkId });
                        this.#UpdateStorage();
                        window.ReloadAccessList(data);
                        MediaStorage.Instance.rebuildMetaList();
                        ok(data);
                    },
                    error: err => ok(false),
                });
            });
        }

        async RebuildAccess() {
            if (this.#linkStoredAccesses.size) {
                return LoadingTasks.push(() => {
                    return this.LinkLogin(Array.from(this.#linkStoredAccesses).map(x => x.value));
                });
            } else {
                return LoadingTasks.push(() => {
                    return new Promise(ok => {
                        $.get("/api/access/list", data => {
                            for (let i in data)
                                this.#linkStoredAccesses.add({ key: i, value: data[i].linkId });
                            this.#UpdateStorage();
                            window.ReloadAccessList(data);
                            MediaStorage.Instance.rebuildMetaList();
                            ok();
                        });
                    });
                });
            }
        }

    }

    window.AccessManager = new AccessManager();
});

