"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Functional_1 = require("../../../../../../Class/Functional");
const AudioNode_1 = require("../../../../../../wrap/AudioNode");
const AudioParam_1 = require("../../../../../../wrap/AudioParam");
const _ids_1 = require("../../../../../_ids");
class OscillatorNode_ extends Functional_1.Functional {
    constructor(system) {
        super({
            i: ['node', 'opt'],
            o: ['node', 'frequency', 'detune'],
        }, {
            input: {
                node: {
                    ref: true,
                },
            },
            output: {
                node: {
                    ref: true,
                },
                frequency: {
                    ref: true,
                },
                detune: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_OSCILLATOR_NODE);
        this._destroy = () => {
            if (this._node) {
                this._node.stop();
                this._node = undefined;
            }
        };
        this.addListener('destroy', () => {
            this._destroy();
        });
    }
    f({ node: sourceNode, opt }, done) {
        let _node;
        const { api: { window: { OscillatorNode }, }, } = this.__system;
        if (sourceNode.__.includes('AC')) {
            _node = sourceNode.createOscilator(opt);
        }
        else {
            sourceNode = sourceNode;
            const context = sourceNode.getContext();
            // @ts-ignore
            _node = new OscillatorNode(context, opt);
        }
        _node.start();
        this._node = _node;
        const node = (0, AudioNode_1.wrapAudioNode)(_node, this.__system);
        const frequency = (0, AudioParam_1.wrapAudioParam)(_node.frequency, this.__system);
        const detune = (0, AudioParam_1.wrapAudioParam)(_node.detune, this.__system);
        done({
            node,
            frequency,
            detune,
        });
    }
    i(name) {
        this._destroy();
    }
    d() {
        this._destroy();
    }
}
exports.default = OscillatorNode_;
//# sourceMappingURL=index.js.map