import { HOST, HTTP_PROTOCOL } from "../Config";
import AnalyticsBaseModel from "../../Models/AnalyticsModels/AnalyticsBaseModel/AnalyticsBaseModel";
import { RealtimeRoute } from "../../ServiceRoute";
import URL from "../../Types/API/URL";
import DatabaseType from "../../Types/BaseDatabase/DatabaseType";
import ObjectID from "../../Types/ObjectID";
import RealtimeUtil from "../../Utils/Realtime";
import SocketIO from "socket.io-client";
import EventName from "../../Types/Realtime/EventName";
export default class Realtime {
    static init() {
        const socket = SocketIO(new URL(HTTP_PROTOCOL, HOST).toString(), {
            path: RealtimeRoute.toString(),
        });
        this.socket = socket;
    }
    static listenToModelEvent(listenToModelEvent, onEvent) {
        // conver this to json and send it to the server.
        if (!this.socket) {
            this.init();
        }
        if (!listenToModelEvent.tenantId) {
            return () => {
                // Do nothing.
            };
        }
        const listenToModelEventJSON = {
            eventType: listenToModelEvent.eventType,
            modelType: DatabaseType.Database,
            modelName: listenToModelEvent.modelType.name,
            tenantId: listenToModelEvent.tenantId.toString(),
        };
        this.emit(EventName.ListenToModalEvent, listenToModelEventJSON);
        const roomId = RealtimeUtil.getRoomId(listenToModelEvent.tenantId, listenToModelEvent.modelType.name, listenToModelEvent.eventType);
        this.socket.on(roomId, (data) => {
            var _a;
            const id = ObjectID.fromString((_a = data["modelId"]) === null || _a === void 0 ? void 0 : _a.toString());
            onEvent(id);
        });
        // Stop listening to the event.
        const stopListening = () => {
            this.socket.off(roomId);
        };
        return stopListening;
    }
    static listenToAnalyticsModelEvent(listenToModelEvent, onEvent) {
        if (!this.socket) {
            this.init();
        }
        const listenToModelEventJSON = {
            eventType: listenToModelEvent.eventType,
            modelType: DatabaseType.AnalyticsDatabase,
            modelName: listenToModelEvent.modelType.name,
            tenantId: listenToModelEvent.tenantId.toString(),
        };
        this.emit(EventName.ListenToModalEvent, listenToModelEventJSON);
        let roomId = RealtimeUtil.getRoomId(listenToModelEvent.tenantId, listenToModelEvent.modelType.name, listenToModelEvent.eventType);
        if (listenToModelEvent.modelId) {
            roomId = RealtimeUtil.getRoomId(listenToModelEvent.tenantId, listenToModelEvent.modelType.name, listenToModelEvent.eventType, listenToModelEvent.modelId);
        }
        this.socket.on(roomId, (model) => {
            onEvent(AnalyticsBaseModel.fromJSON(model, listenToModelEvent.modelType));
        });
        // Stop listening to the event.
        const stopListening = () => {
            this.socket.off(roomId);
        };
        return stopListening;
    }
    static emit(eventName, data) {
        if (!this.socket) {
            this.init();
        }
        this.socket.emit(eventName, data);
    }
}
//# sourceMappingURL=Realtime.js.map