"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Functional_1 = require("../../../../../../Class/Functional");
const AudioNode_1 = require("../../../../../../wrap/AudioNode");
const _ids_1 = require("../../../../../_ids");
class MediaStreamAudioSourceNode_ extends Functional_1.Functional {
    constructor(system) {
        super({
            i: ['node', 'opt', 'stream'],
            o: ['node'],
        }, {
            input: {
                node: {
                    ref: true,
                },
                stream: {
                    ref: true,
                },
            },
            output: {
                node: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_MEDIA_STREAM_AUDIO_SOURCE_NODE);
        this._destroy = () => {
            if (this._node) {
                this._node = undefined;
            }
        };
        this.addListener('destroy', () => {
            this._destroy();
        });
    }
    f({ node: sourceNode, stream, opt }, done) {
        let _node;
        const { api: { window: { MediaStreamAudioSourceNode }, }, } = this.__system;
        let context;
        if (sourceNode.__.includes('AC')) {
            sourceNode = sourceNode;
            context = sourceNode.get();
        }
        else {
            sourceNode = sourceNode;
            context = sourceNode.getContext();
        }
        stream.mediaStream((mediaStream) => {
            try {
                // @ts-ignore
                _node = new MediaStreamAudioSourceNode(context, {
                    mediaStream,
                });
            }
            catch (err) {
                done(undefined, err.message.toLowerCase());
                return;
            }
            this._node = _node;
            const node = (0, AudioNode_1.wrapAudioNode)(_node, this.__system);
            done({
                node,
            });
        });
    }
    i(name) {
        this._destroy();
    }
    d() {
        this._destroy();
    }
}
exports.default = MediaStreamAudioSourceNode_;
//# sourceMappingURL=index.js.map