
import { isHotReloadEnabled, registerHotReloadHandler } from './hotReload.js';
import './observableInternal/index.js';
import { observableSignalFromEvent, constObservable } from './observableInternal/utils.js';
import { observableValue } from './observableInternal/base.js';

function readHotReloadableExport(value, reader) {
    observeHotReloadableExports([value], reader);
    return value;
}
function observeHotReloadableExports(values, reader) {
    if (isHotReloadEnabled()) {
        const o = observableSignalFromEvent('reload', event => registerHotReloadHandler(({ oldExports }) => {
            if (!( [...( Object.values(oldExports))].some(v => values.includes(v)))) {
                return undefined;
            }
            return (_newExports) => {
                event(undefined);
                return true;
            };
        }));
        o.read(reader);
    }
}
const classes = ( new Map());
function createHotClass(clazz) {
    if (!isHotReloadEnabled()) {
        return constObservable(clazz);
    }
    const id = clazz.name;
    let existing = classes.get(id);
    if (!existing) {
        existing = observableValue(id, clazz);
        classes.set(id, existing);
    }
    else {
        setTimeout(() => {
            existing.set(clazz, undefined);
        }, 0);
    }
    return existing;
}

export { createHotClass, observeHotReloadableExports, readHotReloadableExport };
