import { Obj } from '@ephox/katamari';
import { Attribute, Class } from '@ephox/sugar';
// TYPIFY
const findRoute = (component, transConfig, transState, route) => {
    return Obj.get(transConfig.routes, route.start).bind((sConfig) => Obj.get(sConfig, route.destination));
};
const getTransition = (comp, transConfig, transState) => {
    const route = getCurrentRoute(comp, transConfig, transState);
    return route.bind((r) => getTransitionOf(comp, transConfig, transState, r));
};
const getTransitionOf = (comp, transConfig, transState, route) => findRoute(comp, transConfig, transState, route).bind((r) => r.transition.map((t) => ({
    transition: t,
    route: r
})));
const disableTransition = (comp, transConfig, transState) => {
    // Disable the current transition
    getTransition(comp, transConfig, transState).each((routeTransition) => {
        const t = routeTransition.transition;
        Class.remove(comp.element, t.transitionClass);
        Attribute.remove(comp.element, transConfig.destinationAttr);
    });
};
const getNewRoute = (comp, transConfig, transState, destination) => ({
    // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
    start: Attribute.get(comp.element, transConfig.stateAttr),
    destination
});
const getCurrentRoute = (comp, transConfig, _transState) => {
    const el = comp.element;
    return Attribute.getOpt(el, transConfig.destinationAttr).map((destination) => ({
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        start: Attribute.get(comp.element, transConfig.stateAttr),
        destination
    }));
};
const jumpTo = (comp, transConfig, transState, destination) => {
    // Remove the previous transition
    disableTransition(comp, transConfig, transState);
    // Only call finish if there was an original state
    if (Attribute.has(comp.element, transConfig.stateAttr) && Attribute.get(comp.element, transConfig.stateAttr) !== destination) {
        transConfig.onFinish(comp, destination);
    }
    Attribute.set(comp.element, transConfig.stateAttr, destination);
};
const fasttrack = (comp, transConfig, _transState, _destination) => {
    if (Attribute.has(comp.element, transConfig.destinationAttr)) {
        Attribute.getOpt(comp.element, transConfig.destinationAttr).each((destination) => {
            Attribute.set(comp.element, transConfig.stateAttr, destination);
        });
        Attribute.remove(comp.element, transConfig.destinationAttr);
    }
};
const progressTo = (comp, transConfig, transState, destination) => {
    fasttrack(comp, transConfig, transState, destination);
    const route = getNewRoute(comp, transConfig, transState, destination);
    getTransitionOf(comp, transConfig, transState, route).fold(() => {
        jumpTo(comp, transConfig, transState, destination);
    }, (routeTransition) => {
        disableTransition(comp, transConfig, transState);
        const t = routeTransition.transition;
        Class.add(comp.element, t.transitionClass);
        Attribute.set(comp.element, transConfig.destinationAttr, destination);
    });
};
const getState = (comp, transConfig, _transState) => Attribute.getOpt(comp.element, transConfig.stateAttr);
export { findRoute, disableTransition, getCurrentRoute, jumpTo, progressTo, getState };
//# sourceMappingURL=TransitionApis.js.map