"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 _1 = require("../../../../../Class/$");
const Semifunctional_1 = require("../../../../../Class/Semifunctional");
const _ids_1 = require("../../../../_ids");
class _MediaRecorder extends Semifunctional_1.Semifunctional {
    constructor(system) {
        super({
            fi: ['opt', 'stream'],
            fo: [],
            i: ['start', 'stop'],
            o: ['blob'],
        }, {
            input: {
                stream: {
                    ref: true,
                },
            },
            output: {
                blob: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_MEDIA_RECORDER);
        this._start_recorder = (start) => {
            if (this._media_recorder) {
                if (this._media_recorder.state === 'recording') {
                    this._media_recorder.stop();
                }
                this._media_recorder.start(start);
            }
        };
        this.addListener('destroy', () => {
            this._media_recorder.stop();
            this._media_recorder.ondataavailable = null;
            this._media_recorder = null;
        });
    }
    f({ opt, stream }) {
        const chunks = [];
        stream.mediaStream((_stream) => {
            if (this._media_recorder) {
                this._media_recorder.stop();
                this._media_recorder.ondataavailable = null;
            }
            this._media_recorder = new MediaRecorder(_stream, opt);
            this._media_recorder.ondataavailable = (event) => {
                const { data } = event;
                if (data.size > 0) {
                    chunks.push(data);
                }
            };
            this._media_recorder.onstop = () => {
                const data = new Blob(chunks, { type: 'audio/wav' });
                const _blob = new (class _Blob extends _1.$ {
                    constructor() {
                        super(...arguments);
                        this.__ = ['B', 'IM'];
                    }
                    image() {
                        return __awaiter(this, void 0, void 0, function* () {
                            return data;
                        });
                    }
                    blob() {
                        return __awaiter(this, void 0, void 0, function* () {
                            return data;
                        });
                    }
                })(this.__system);
                this._output.blob.push(_blob);
            };
            const start = this._input.start.peak();
            if (start !== undefined) {
                this._start_recorder(start);
            }
        });
    }
    onIterDataInputData(name, data) {
        if (name === 'start') {
            if (this._media_recorder) {
                this._start_recorder(data);
            }
        }
        else if (name === 'stop') {
            if (this._media_recorder) {
                if (this._media_recorder.state === 'recording') {
                    this._media_recorder.stop();
                }
                this._backward('start');
                this._backward('stop');
            }
        }
    }
    onIterDataInputDrop(name) {
        if (this.hasErr()) {
            this.takeErr();
        }
        if (name === 'start') {
            if (!this._backwarding) {
                if (this._media_recorder) {
                    this._media_recorder.stop();
                }
            }
        }
    }
}
exports.default = _MediaRecorder;
//# sourceMappingURL=index.js.map