import { Fun, Obj } from '@ephox/katamari';
import { nuState } from '../common/BehaviourState';
const init = () => {
    const coupled = {};
    const getOrCreate = (component, coupleConfig, name) => {
        const available = Obj.keys(coupleConfig.others);
        if (!available) {
            throw new Error('Cannot find coupled component: ' + name + '. Known coupled components: ' + JSON.stringify(available, null, 2));
        }
        else {
            // TODO: Likely type error. coupleConfig.others[key] is `() => ((comp: AlloyComponent) => AlloySpec)`, but builder is being treated as a `(comp: AlloyComponent) => AlloySpec`
            return Obj.get(coupled, name).getOrThunk(() => {
                const builder = Obj.get(coupleConfig.others, name).getOrDie('No information found for coupled component: ' + name);
                const spec = builder(component);
                const built = component.getSystem().build(spec);
                coupled[name] = built;
                return built;
            });
        }
    };
    const readState = Fun.constant({});
    return nuState({
        readState,
        getOrCreate
    });
};
export { init };
//# sourceMappingURL=CouplingState.js.map