"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const _1 = require("../../../../../Class/$");
const Semifunctional_1 = require("../../../../../Class/Semifunctional");
const id_1 = require("../../../../../util/id");
const _ids_1 = require("../../../../_ids");
class Animate extends Semifunctional_1.Semifunctional {
    constructor(system) {
        super({
            fi: ['opt', 'keyframes', 'element'],
            fo: ['animation'],
            i: ['done'],
            o: [],
        }, {
            input: {
                element: {
                    ref: true,
                },
                done: {},
            },
            output: {
                animation: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_ANIMATE);
        this._id = undefined;
        this.addListener('reset', this._cancel);
        this.addListener('destroy', this._cancel);
    }
    _cancel() {
        if (this._i.element) {
            if (this._id) {
                this._i.element.cancelAnimation(this._id);
                this._id = undefined;
            }
        }
    }
    f({ opt, keyframes, element }, done) {
        var _a;
        const id = (_a = opt.id) !== null && _a !== void 0 ? _a : (0, id_1.randomId)();
        this._id = id;
        const _opt = Object.assign(Object.assign({}, opt), { id });
        element.animate(keyframes, _opt);
        const animation = new (class Node extends _1.$ {
            constructor() {
                super(...arguments);
                this.__ = ['ANI'];
            }
            stop() {
                element.emit('call', {
                    method: 'stopAnimation',
                    data: [id],
                });
            }
            play() {
                element.emit('call', {
                    method: 'playAnimation',
                    data: [id],
                });
            }
            cancel() {
                element.emit('call', {
                    method: 'cancelAnimation',
                    data: [id],
                });
            }
            finish() {
                element.emit('call', {
                    method: 'finishAnimation',
                    data: [id],
                });
            }
            commit() {
                element.emit('call', {
                    method: 'commitAnimation',
                    data: [id],
                });
            }
        })(this.__system);
        done({ animation });
    }
    fd() {
        this._cancel();
    }
    fi() {
        this._cancel();
    }
    onRefInputDrop(name, data) {
        if (name === 'element') {
            this._cancel();
        }
    }
    onIterDataInputData(name, data) {
        // if (name === 'done') {
        this._cancel();
        this._id = undefined;
        this._forward_empty('animation');
        this._backward('keyframes');
        this._backward('opt');
        this._backward('done');
        // }
    }
}
exports.default = Animate;
//# sourceMappingURL=index.js.map