import { Events } from "../../events/Events.js";
import { EventTypes } from "../../events/EventTypes.js";
import { StoreBroadcast } from "../../http-client/store/StoreBroadcast.js";
import { PlatformBindings } from "./PlatformBindings.js";
export class HttpClientStoreBindings extends PlatformBindings {
    constructor() {
        super(...arguments);
        this.urlHistoryChannel = new BroadcastChannel(StoreBroadcast.urlHistory);
    }
    async initialize() {
        const target = document.body;
        target.addEventListener(EventTypes.HttpClient.Model.UrlHistory.add, this.addUrlHistoryHandler.bind(this));
        target.addEventListener(EventTypes.HttpClient.Model.UrlHistory.query, this.queryUrlHistoryHandler.bind(this));
        target.addEventListener(EventTypes.HttpClient.Model.UrlHistory.delete, this.deleteUrlHistoryHandler.bind(this));
        target.addEventListener(EventTypes.HttpClient.Model.UrlHistory.clear, this.clearUrlHistoryHandler.bind(this));
        target.addEventListener(EventTypes.HttpClient.Model.WsHistory.add, this.addWsHistoryHandler.bind(this));
        target.addEventListener(EventTypes.HttpClient.Model.WsHistory.query, this.queryWsHistoryHandler.bind(this));
        target.addEventListener(EventTypes.HttpClient.Model.WsHistory.delete, this.deleteWsHistoryHandler.bind(this));
        target.addEventListener(EventTypes.HttpClient.Model.WsHistory.clear, this.clearWsHistoryHandler.bind(this));
        // Request UI
        window.addEventListener(EventTypes.HttpProject.Ui.delete, this.deleteProjectUiHandler.bind(this));
        window.addEventListener(EventTypes.HttpProject.Ui.HttpRequest.set, this.setHttpRequestUiHandler.bind(this));
        window.addEventListener(EventTypes.HttpProject.Ui.HttpRequest.get, this.getHttpRequestUiHandler.bind(this));
        window.addEventListener(EventTypes.HttpProject.Ui.HttpRequest.getBulk, this.getBulkHttpRequestUiHandler.bind(this));
        window.addEventListener(EventTypes.HttpProject.Ui.HttpRequest.delete, this.deleteHttpRequestUiHandler.bind(this));
        this.urlHistoryChannel.addEventListener('message', this.handleUrlHistoryBroadcast.bind(this));
    }
    handleUrlHistoryBroadcast(e) {
        const event = e.data;
        switch (event.operation) {
            case 'created':
                this.handleUrlHistoryCreated(event.data);
                break;
            case 'deleted':
                this.handleUrlHistoryDeleted(event.key);
                break;
            case 'cleared':
                this.handleUrlHistoryCleared();
                break;
            default:
        }
    }
    handleUrlHistoryCreated(entity) {
        Events.HttpClient.Model.UrlHistory.State.add(entity);
    }
    handleUrlHistoryDeleted(url) {
        Events.HttpClient.Model.UrlHistory.State.delete(url);
    }
    handleUrlHistoryCleared() {
        Events.HttpClient.Model.UrlHistory.State.clear();
    }
    addUrlHistoryHandler(event) {
        const e = event;
        e.preventDefault();
        e.detail.result = this.urlHistory.add(e.detail.url);
    }
    queryUrlHistoryHandler(event) {
        const e = event;
        e.preventDefault();
        e.detail.result = this.urlHistory.query(e.detail.term);
    }
    deleteUrlHistoryHandler(event) {
        const e = event;
        e.preventDefault();
        e.detail.result = this.urlHistory.delete(e.detail.key);
    }
    clearUrlHistoryHandler(event) {
        const e = event;
        e.preventDefault();
        e.detail.result = this.urlHistory.clear();
    }
    addWsHistoryHandler(event) {
        const e = event;
        e.preventDefault();
        e.detail.result = this.wsHistory.add(e.detail.url);
    }
    queryWsHistoryHandler(event) {
        const e = event;
        e.preventDefault();
        e.detail.result = this.wsHistory.query(e.detail.term);
    }
    deleteWsHistoryHandler(event) {
        const e = event;
        e.preventDefault();
        e.detail.result = this.wsHistory.delete(e.detail.key);
    }
    clearWsHistoryHandler(event) {
        const e = event;
        e.preventDefault();
        e.detail.result = this.wsHistory.clear();
    }
    deleteProjectUiHandler(e) {
        e.preventDefault();
        const { key } = e.detail;
        e.detail.result = this.projectUi.clear(key);
    }
    setHttpRequestUiHandler(e) {
        e.preventDefault();
        const { item } = e.detail;
        e.detail.result = this.projectUi.set(item.pid, item.id, item.meta);
    }
    getHttpRequestUiHandler(e) {
        e.preventDefault();
        const { key, parent } = e.detail;
        e.detail.result = this.projectUi.get(parent, key);
    }
    getBulkHttpRequestUiHandler(e) {
        e.preventDefault();
        const { keys, parent } = e.detail;
        e.detail.result = this.projectUi.getBulk(parent, keys);
    }
    deleteHttpRequestUiHandler(e) {
        e.preventDefault();
        const { key, parent } = e.detail;
        e.detail.result = this.projectUi.delete(parent, key);
    }
}
//# sourceMappingURL=HttpClientStoreBindings.js.map