"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Functional_1 = require("../../../../../Class/Functional");
const APINotImplementedError_1 = require("../../../../../exception/APINotImplementedError");
const _ids_1 = require("../../../../_ids");
class RequestAnimationFrame extends Functional_1.Functional {
    constructor(system) {
        super({
            i: ['a'],
            o: ['a'],
        }, {}, system, _ids_1.ID_REQUEST_ANIMATION_FRAME);
        this._frame = undefined;
    }
    f({ a }, done) {
        const { api: { animation: { requestAnimationFrame }, }, } = this.__system;
        if (!requestAnimationFrame) {
            done(undefined, (0, APINotImplementedError_1.apiNotSupportedError)('Request Animation Frame'));
            return;
        }
        this._frame = requestAnimationFrame(() => {
            this._frame = undefined;
            done({ a });
        });
    }
    d() {
        const { api: { animation: { cancelAnimationFrame }, }, } = this.__system;
        if (this._frame) {
            cancelAnimationFrame(this._frame);
            this._frame = undefined;
        }
    }
    snapshotSelf() {
        return {
            ...super.snapshotSelf(),
            _frame: this._frame,
        };
    }
    restoreSelf(state) {
        const { _frame, ...rest } = state;
        super.restoreSelf(rest);
        if (_frame !== undefined) {
            this._frame = requestAnimationFrame(() => {
                this._frame = undefined;
                this._done({ a: this._i.a });
            });
        }
    }
}
exports.default = RequestAnimationFrame;
//# sourceMappingURL=index.js.map