"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const _1 = require("../../../../../../Class/$");
const Holder_1 = require("../../../../../../Class/Holder");
const _ids_1 = require("../../../../../_ids");
class BufferSourceNode extends Holder_1.Holder {
    constructor(system) {
        super({
            fi: ['buffer', 'context'],
            fo: ['node'],
        }, {
            input: {
                buffer: {
                    ref: true,
                },
                context: {
                    ref: true,
                },
            },
            output: {
                node: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_BUFFER_SOURCE_NODE);
    }
    async f({ buffer, context }, done) {
        let node_;
        const buffer_ = await buffer.audioBuffer();
        node_ = context.createBufferSource();
        node_.buffer = buffer_;
        const node = new (class BufferSourceNode_ extends _1.$ {
            constructor() {
                super(...arguments);
                this.__ = ['AN', 'BSN'];
            }
            getContext() {
                return node_.context;
            }
            connect(targetAudioNode) {
                node_.connect(targetAudioNode);
            }
            disconnect(targetAudioNode) {
                node_.disconnect(targetAudioNode);
            }
            start(when, offset, duration) {
                node_.start(when, offset, duration);
            }
            addListener(event, listener) {
                if (event === 'ended') {
                    const listener_ = (event) => {
                        listener({});
                    };
                    node_.addEventListener('ended', listener_);
                    return () => {
                        node_.removeEventListener('ended', listener_);
                    };
                }
                return super.addListener(event, listener);
            }
        })(this.__system);
        done({
            node,
        });
    }
    d() {
        if (this._node) {
            this._node = undefined;
        }
    }
}
exports.default = BufferSourceNode;
//# sourceMappingURL=index.js.map