"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const Semifunctional_1 = require("../../../../../Class/Semifunctional");
const ImageBitmap_1 = require("../../../../../wrap/ImageBitmap");
const _ids_1 = require("../../../../_ids");
class GrabFrame extends Semifunctional_1.Semifunctional {
    constructor(system) {
        super({
            fi: ['camera'],
            fo: [],
            i: ['init', 'done'],
            o: ['image'],
        }, {
            input: {
                camera: {
                    ref: true,
                },
            },
            output: {
                image: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_GRAB_FRAME);
        this._err_flag = false;
        this._forward_if_ready = () => __awaiter(this, void 0, void 0, function* () {
            if (this._input.camera.active() && this._input.init.active()) {
                let _image;
                try {
                    _image = yield this._i.camera.grabFrame();
                }
                catch (err) {
                    this.err(err.message.toLowerCase());
                    this._err_flag = true;
                    return;
                }
                const image = (0, ImageBitmap_1.wrapImageBitmap)(_image, this.__system);
                this._output.image.push(image);
                this._input.init.pull();
            }
        });
        this.addListener('destroy', () => { });
        this.addListener('take_err', () => {
            if (this._err_flag) {
                this._err_flag = false;
                this._input.init.pull();
            }
        });
    }
    f({ camera }, done) {
        return __awaiter(this, void 0, void 0, function* () {
            this._forward_if_ready();
        });
    }
    onIterDataInputData(name) {
        return __awaiter(this, void 0, void 0, function* () {
            if (name === 'done') {
                this._forward_empty('image');
                this._done();
                this._backward('done');
            }
            else if (name === 'init') {
                this._forward_if_ready();
            }
        });
    }
}
exports.default = GrabFrame;
//# sourceMappingURL=index.js.map