/*
 * Copyright (C) 2016-2020 Kano Computing Ltd.
 * License: http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
 */
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { Part } from '../../part.js';
import { PartComponent } from '../../component.js';
import { component, property, part } from '../../decorators.js';
import { Wave } from './wave.js';
import { transformLegacyOscillator } from './legacy.js';
/**
 * Set of function that returns a y for a given x
 */
const WAVES = {
    sine(x) {
        const angle = ((x * 3.6) * Math.PI) / 180;
        // Normalize to a range of 0 - 100
        return ((Math.sin(angle) * 100) / 2) + 50;
    },
    square(x) {
        x %= 100;
        return x > 50 ? 100 : 0;
    },
    sawtooth(x) {
        return x % 100;
    },
    triangle(x) {
        x %= 100;
        return x > 50 ? 100 - x : x;
    },
};
class OscillatorComponent extends PartComponent {
    constructor() {
        super(...arguments);
        this.x = 0;
        this.delay = 0;
        this.speed = 50;
        this.wave = 'sine';
    }
}
__decorate([
    property({ type: Number, value: 0 })
], OscillatorComponent.prototype, "x", void 0);
__decorate([
    property({ type: Number, value: 0 })
], OscillatorComponent.prototype, "delay", void 0);
__decorate([
    property({ type: Number, value: 50 })
], OscillatorComponent.prototype, "speed", void 0);
__decorate([
    property({ type: Wave, value: 50 })
], OscillatorComponent.prototype, "wave", void 0);
let OscillatorPart = class OscillatorPart extends Part {
    constructor() {
        super();
        this.timeout = null;
        this.core = this._components.get('core');
    }
    static transformLegacy(app) {
        transformLegacyOscillator(app);
    }
    onStart() {
        if (!this.timeout) {
            this._updateValue();
        }
    }
    onStop() {
        this._stopOscillating();
    }
    _updateValue() {
        if (this.timeout) {
            this._stopOscillating();
        }
        // TODO: Use a requestAnimation frame and apply the delta time to the step
        // this would make the oscillator behave normally even under pressure
        // Investigate if it would have an impact on past creations
        // Start a 60 tick per second timeout recursive loop.
        this.timeout = window.setTimeout(() => {
            // Increment x by one step
            this.core.x += this._getStep();
            this._checkBounds();
            this.core.invalidate();
            this._updateValue();
        }, 16);
    }
    /**
     * Stop the oscillation. This will cancel the resursive timeout loop
     */
    _stopOscillating() {
        if (this.timeout) {
            clearTimeout(this.timeout);
        }
        this.timeout = null;
    }
    _getStep() {
        return this.core.speed / 20;
    }
    get speed() {
        return this.core.speed;
    }
    set speed(s) {
        s = Math.min(100, Math.max(0, s));
        this.core.speed = s;
        this.core.invalidate();
    }
    get delay() {
        return this.core.delay;
    }
    set delay(d) {
        d = Math.min(100, Math.max(0, d));
        this.core.delay = d;
        this.core.invalidate();
    }
    get value() {
        return this.getValueAt(this.core.x);
    }
    set wave(wave) {
        this.core.wave = wave;
        this.core.invalidate();
    }
    /**
     * Clamps x between 0 and 100
     * It mutates core.x but does not invalidate
     */
    _checkBounds() {
        if (this.core.x > 100) {
            this.core.x = 101 - this.core.x;
        }
        else if (this.core.x < 0) {
            this.core.x = 101 + this.core.x;
        }
    }
    getValueAt(x) {
        const xWithOffset = this.core.delay + x;
        const fn = WAVES[this.core.wave];
        if (typeof fn === 'function') {
            return fn(xWithOffset);
        }
        return WAVES.sine(xWithOffset);
    }
};
__decorate([
    component(OscillatorComponent)
], OscillatorPart.prototype, "core", void 0);
OscillatorPart = __decorate([
    part('oscillator')
], OscillatorPart);
export { OscillatorPart };
//# sourceMappingURL=oscillator.js.map