"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LOCAL_STORAGE_PREFIX_BROADCAST_TARGET = exports.LOCAL_STORAGE_PREFIX_BROADCAST_SOURCE = void 0;
exports.localBroadcastSourceName = localBroadcastSourceName;
exports.localBroadcastTargetName = localBroadcastTargetName;
exports.hasLocalBroadcastSource = hasLocalBroadcastSource;
exports.hasLocalBroadcastTarget = hasLocalBroadcastTarget;
exports.startBroadcast = startBroadcast;
exports.startBroadcastSource = startBroadcastSource;
exports.startBroadcastTarget = startBroadcastTarget;
exports.stopBroadcastSource = stopBroadcastSource;
exports.stopBroadcastTarget = stopBroadcastTarget;
exports.shareLocalGraph = shareLocalGraph;
const makeUnitRemoteRef_1 = require("../../../client/makeUnitRemoteRef");
const STRING_1 = require("../../../constant/STRING");
const UCGEE_1 = require("../../../types/interface/UCGEE");
const uuidNotInLocalStorage_1 = require("./uuidNotInLocalStorage");
exports.LOCAL_STORAGE_PREFIX_BROADCAST_SOURCE = '__BROADCAST__SOURCE__';
exports.LOCAL_STORAGE_PREFIX_BROADCAST_TARGET = '__BROADCAST__TARGET__';
function localBroadcastSourceName(id) {
    return `${exports.LOCAL_STORAGE_PREFIX_BROADCAST_SOURCE}/${id}`;
}
function localBroadcastTargetName(id) {
    return `${exports.LOCAL_STORAGE_PREFIX_BROADCAST_TARGET}/${id}`;
}
function hasLocalBroadcastSource(id) {
    const _id = localBroadcastSourceName(id);
    const has = localStorage.getItem(_id) !== null;
    return has;
}
function hasLocalBroadcastTarget(id) {
    const _id = localBroadcastTargetName(id);
    const has = localStorage.getItem(_id) !== null;
    return has;
}
function startBroadcast(prefix) {
    const [id, _id] = (0, uuidNotInLocalStorage_1.uuidNotInLocalStorage)(prefix);
    localStorage.setItem(_id, '');
    const channel = new BroadcastChannel(_id);
    return { channel, id };
}
function startBroadcastSource() {
    return startBroadcast(exports.LOCAL_STORAGE_PREFIX_BROADCAST_SOURCE);
}
function startBroadcastTarget() {
    return startBroadcast(exports.LOCAL_STORAGE_PREFIX_BROADCAST_TARGET);
}
function stopBroadcastSource(id) {
    const _id = localBroadcastSourceName(id);
    localStorage.removeItem(_id);
}
function stopBroadcastTarget(id) {
    const _id = localBroadcastTargetName(id);
    localStorage.removeItem(_id);
}
function shareLocalGraph(graph) {
    const { channel: broadcastChannel, id } = startBroadcastSource();
    const _target = {};
    broadcastChannel.addEventListener('message', (event) => {
        const { data } = event;
        const { type, data: _data } = data;
        switch (type) {
            case STRING_1.CONNECT:
                {
                    const name = _data;
                    const bc = new BroadcastChannel(name);
                    const ref = (0, makeUnitRemoteRef_1.makeUnitRemoteRef)(graph, UCGEE_1.UCGEE, (data) => {
                        bc.postMessage({ type: STRING_1.EXEC, data });
                    });
                    bc.addEventListener('message', (event) => {
                        const { data } = event;
                        ref.exec(data);
                    });
                    _target[id] = { bc, ref };
                }
                break;
            case STRING_1.DISCONNECT:
                {
                    const id = _data;
                    delete _target[id];
                }
                break;
            default:
                throw new Error('invalid message type');
        }
    });
    const terminate = () => {
        for (const id in _target) {
            const target = _target[id];
            const { bc } = target;
            bc.postMessage({ type: STRING_1.TERMINATE, data: {} });
            bc.close();
        }
        broadcastChannel.close();
    };
    return { id, terminate };
}
//# sourceMappingURL=index.js.map