"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.shareLocalGraph = exports.stopBroadcastTarget = exports.stopBroadcastSource = exports.startBroadcastTarget = exports.startBroadcastSource = exports.startBroadcast = exports.hasLocalBroadcastTarget = exports.hasLocalBroadcastSource = exports.localBroadcastTargetName = exports.localBroadcastSourceName = exports.LOCAL_STORAGE_PREFIX_BROADCAST_TARGET = exports.LOCAL_STORAGE_PREFIX_BROADCAST_SOURCE = void 0;
const makeUnitRemoteRef_1 = require("../../../client/makeUnitRemoteRef");
const STRING_1 = require("../../../constant/STRING");
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}`;
}
exports.localBroadcastSourceName = localBroadcastSourceName;
function localBroadcastTargetName(id) {
    return `${exports.LOCAL_STORAGE_PREFIX_BROADCAST_TARGET}/${id}`;
}
exports.localBroadcastTargetName = localBroadcastTargetName;
function hasLocalBroadcastSource(id) {
    const _id = localBroadcastSourceName(id);
    const has = localStorage.getItem(_id) !== null;
    return has;
}
exports.hasLocalBroadcastSource = hasLocalBroadcastSource;
function hasLocalBroadcastTarget(id) {
    const _id = localBroadcastTargetName(id);
    const has = localStorage.getItem(_id) !== null;
    return has;
}
exports.hasLocalBroadcastTarget = hasLocalBroadcastTarget;
function startBroadcast(prefix) {
    const [id, _id] = (0, uuidNotInLocalStorage_1.uuidNotInLocalStorage)(prefix);
    localStorage.setItem(_id, '');
    const channel = new BroadcastChannel(_id);
    return { channel, id };
}
exports.startBroadcast = startBroadcast;
function startBroadcastSource() {
    return startBroadcast(exports.LOCAL_STORAGE_PREFIX_BROADCAST_SOURCE);
}
exports.startBroadcastSource = startBroadcastSource;
function startBroadcastTarget() {
    return startBroadcast(exports.LOCAL_STORAGE_PREFIX_BROADCAST_TARGET);
}
exports.startBroadcastTarget = startBroadcastTarget;
function stopBroadcastSource(id) {
    const _id = localBroadcastSourceName(id);
    localStorage.removeItem(_id);
}
exports.stopBroadcastSource = stopBroadcastSource;
function stopBroadcastTarget(id) {
    const _id = localBroadcastTargetName(id);
    localStorage.removeItem(_id);
}
exports.stopBroadcastTarget = stopBroadcastTarget;
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, ['U', 'C', 'G'], (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 };
}
exports.shareLocalGraph = shareLocalGraph;
//# sourceMappingURL=index.js.map