"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Functional_1 = require("../../../../../../Class/Functional");
const AudioBuffer_1 = require("../../../../../../wrap/AudioBuffer");
const _ids_1 = require("../../../../../_ids");
class DecodeAudioData extends Functional_1.Functional {
    constructor(system) {
        super({
            i: ['buffer', 'context'],
            o: ['buffer'],
        }, {
            input: {
                buffer: {
                    ref: true,
                },
                context: {
                    ref: true,
                },
            },
            output: {
                buffer: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_GAIN_NODE);
    }
    async f({ buffer, context }, done, fail) {
        const arrayBuffer = await buffer.arrayBuffer();
        let audioBuffer;
        // @ts-ignore
        if (arrayBuffer.detached) {
            fail('cannot decode detached array buffer');
            return;
        }
        try {
            const context_ = context.audioContext();
            await context_.resume();
            audioBuffer = await context.decodeAudioData(arrayBuffer);
        }
        catch (err) {
            fail(err.message ?? 'unknown error while decoding audio array buffer');
            return;
        }
        const buffer_ = (0, AudioBuffer_1.wrapAudioBuffer)(audioBuffer, this.__system);
        done({
            buffer: buffer_,
        });
    }
}
exports.default = DecodeAudioData;
//# sourceMappingURL=index.js.map