import { FieldSchema, StructureSchema } from '@ephox/boulder';
import { Receiving } from '../api/behaviour/Receiving';
import { Sandboxing } from '../api/behaviour/Sandboxing';
import * as AlloyTriggers from '../api/events/AlloyTriggers';
import * as SystemEvents from '../api/events/SystemEvents';
import * as Channels from '../api/messages/Channels';
const schema = StructureSchema.objOfOnly([
    FieldSchema.optionObjOf('fireEventInstead', [
        FieldSchema.defaulted('event', SystemEvents.repositionRequested())
    ]),
    FieldSchema.requiredFunction('doReposition')
]);
const receivingConfig = (rawSpec) => {
    const c = receivingChannel(rawSpec);
    return Receiving.config({
        channels: c
    });
};
const receivingChannel = (rawSpec) => {
    const detail = StructureSchema.asRawOrDie('Reposition', schema, rawSpec);
    return {
        [Channels.repositionPopups()]: {
            onReceive: (sandbox) => {
                if (Sandboxing.isOpen(sandbox)) {
                    detail.fireEventInstead.fold(() => detail.doReposition(sandbox), (fe) => AlloyTriggers.emit(sandbox, fe.event));
                }
            }
        }
    };
};
export { receivingChannel, receivingConfig };
//# sourceMappingURL=Reposition.js.map