import { __values } from "tslib";
import * as Core from "@intelligentgraphics/ig.gfx.core";
import { EventEmitter, isValue, noop, } from "@intelligentgraphics/ig.utilities";
import { getPosition as getAnimationPosition } from "./Util";
export var createAnimationManager = function (session, plannerManager, _a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.continueAnimationsAfterUpdate, continueAnimationsAfterUpdate = _c === void 0 ? false : _c, _d = _b.reapplyAnimationProgressAfterUpdate, reapplyAnimationProgressAfterUpdate = _d === void 0 ? false : _d;
    var playbackControls = new Map();
    var eventEmitter = new EventEmitter();
    var getRootIds = function (comIds) {
        return comIds.map(function (comId) { return session.getArticleGfxId(comId); }).filter(isValue);
    };
    var getPosition = function (comIds) {
        var rootIds = getRootIds(comIds);
        return getAnimationPosition(Core.State.API, rootIds);
    };
    var setPosition = function (comIds, position) {
        forEachPlaybackControl(comIds, function (playbackControl) {
            return playbackControl.stop();
        });
        Core.Animation.setPosition(getRootIds(comIds), position === "end" ? "End" : "Start");
        eventEmitter.emit("statusChange", {
            comIds: comIds,
            status: position,
        });
    };
    var showAnimation = function (comIds, params, options) {
        var e_1, _a;
        var playbackControl = Core.Animation.showAnimation(getRootIds(comIds), params, options);
        var createStatusChanger = function (method, status) { return function () {
            playbackControl[method].apply(playbackControl);
            eventEmitter.emit("statusChange", {
                comIds: comIds,
                status: status,
            });
        }; };
        var decoratedPlaybackControl = {
            done: playbackControl.done,
            continue: createStatusChanger("continue", "running"),
            pause: createStatusChanger("pause", "paused"),
            stop: createStatusChanger("stop", "stopped"),
        };
        try {
            for (var comIds_1 = __values(comIds), comIds_1_1 = comIds_1.next(); !comIds_1_1.done; comIds_1_1 = comIds_1.next()) {
                var comId = comIds_1_1.value;
                playbackControls.set(comId, decoratedPlaybackControl);
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (comIds_1_1 && !comIds_1_1.done && (_a = comIds_1.return)) _a.call(comIds_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        eventEmitter.emit("statusChange", {
            comIds: comIds,
            status: "running",
        });
        var newStatus = params.forward === !params.invertAnimationAfter ? "end" : "start";
        playbackControl.done
            .then(function () {
            eventEmitter.emit("statusChange", {
                comIds: comIds,
                status: newStatus,
            });
        })
            .finally(function () {
            var e_2, _a;
            try {
                for (var comIds_2 = __values(comIds), comIds_2_1 = comIds_2.next(); !comIds_2_1.done; comIds_2_1 = comIds_2.next()) {
                    var comId = comIds_2_1.value;
                    playbackControls.delete(comId);
                }
            }
            catch (e_2_1) { e_2 = { error: e_2_1 }; }
            finally {
                try {
                    if (comIds_2_1 && !comIds_2_1.done && (_a = comIds_2.return)) _a.call(comIds_2);
                }
                finally { if (e_2) throw e_2.error; }
            }
        })
            .catch(noop);
    };
    var showAnimationFromCurrentPosition = function (comIds, invertAnimationAfter, options) {
        var position = getPosition(comIds);
        return showAnimation(comIds, { forward: position !== "end", invertAnimationAfter: invertAnimationAfter }, options);
    };
    var getPlaybackControls = function (comIds) {
        var e_3, _a;
        var controls = new Set();
        try {
            for (var comIds_3 = __values(comIds), comIds_3_1 = comIds_3.next(); !comIds_3_1.done; comIds_3_1 = comIds_3.next()) {
                var comId = comIds_3_1.value;
                var control = playbackControls.get(comId);
                if (control !== undefined)
                    controls.add(control);
            }
        }
        catch (e_3_1) { e_3 = { error: e_3_1 }; }
        finally {
            try {
                if (comIds_3_1 && !comIds_3_1.done && (_a = comIds_3.return)) _a.call(comIds_3);
            }
            finally { if (e_3) throw e_3.error; }
        }
        return controls;
    };
    var forEachPlaybackControl = function (comIds, cb) {
        var e_4, _a;
        try {
            for (var _b = __values(getPlaybackControls(comIds)), _c = _b.next(); !_c.done; _c = _b.next()) {
                var playbackControl = _c.value;
                cb(playbackControl);
            }
        }
        catch (e_4_1) { e_4 = { error: e_4_1 }; }
        finally {
            try {
                if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
            }
            finally { if (e_4) throw e_4.error; }
        }
    };
    var pauseAnimation = function (comIds) {
        return forEachPlaybackControl(comIds, function (playbackControl) {
            return playbackControl.pause();
        });
    };
    var continueAnimation = function (comIds) {
        return forEachPlaybackControl(comIds, function (playbackControl) {
            return playbackControl.continue();
        });
    };
    var stopAnimation = function (comIds) {
        return forEachPlaybackControl(comIds, function (playbackControl) {
            return playbackControl.stop();
        });
    };
    var canAnimate = function (comIds) { return getRootIds(comIds).length > 0; };
    return {
        on: eventEmitter.on.bind(eventEmitter),
        off: eventEmitter.off.bind(eventEmitter),
        once: eventEmitter.once.bind(eventEmitter),
        setPosition: setPosition,
        getPosition: getPosition,
        showAnimation: showAnimation,
        showAnimationFromCurrentPosition: showAnimationFromCurrentPosition,
        pauseAnimation: pauseAnimation,
        continueAnimation: continueAnimation,
        stopAnimation: stopAnimation,
        canAnimate: canAnimate,
        getPlaybackControls: getPlaybackControls,
    };
};
