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 { part, property, component } from '../../decorators.js';
import { PartComponent } from '../../component.js';
import { subscribeInterval } from '@kano/common/index.js';
import { transformLegacyMicrophone } from './legacy.js';
class MicrophoneComponent extends PartComponent {
    constructor() {
        super(...arguments);
        this.volume = 0;
        this.pitch = 0;
    }
}
__decorate([
    property({ type: Number, value: 0 })
], MicrophoneComponent.prototype, "volume", void 0);
__decorate([
    property({ type: Number, value: 0 })
], MicrophoneComponent.prototype, "pitch", void 0);
let MicrophonePart = class MicrophonePart extends Part {
    constructor() {
        super();
        this.thresholds = [];
        this.thresholdSub = null;
        this.core = this._components.get('core');
    }
    static transformLegacy(app) {
        transformLegacyMicrophone(app);
    }
    onInstall(context) {
        this.microphone = context.audio.microphone;
        this.microphone.start();
    }
    onStop() {
        if (this.thresholdSub) {
            this.thresholdSub.dispose();
        }
        this.thresholdSub = null;
        this.thresholds.length = 0;
    }
    _queryVolume() {
        if (this.microphone && this.microphone.started) {
            const currentVolume = this.core.volume;
            const volume = this.microphone.volume;
            if (currentVolume !== volume) {
                this.core.volume = this.microphone.volume;
                this.core.invalidate();
            }
        }
    }
    _queryPitch() {
        if (this.microphone && this.microphone.started) {
            const currentPitch = this.core.pitch;
            const pitch = this.microphone.pitch;
            if (currentPitch !== pitch) {
                this.core.pitch = this.microphone.pitch;
                this.core.invalidate();
            }
        }
    }
    get volume() {
        this._queryVolume();
        return this.core.volume;
    }
    get pitch() {
        this._queryPitch();
        return this.core.pitch;
    }
    _executeThresholds() {
        this.thresholds.forEach((threshold) => {
            const isOver = this.volume > threshold.value;
            const isActive = threshold.active;
            const willBeActive = (threshold.rising && isOver) || (!threshold.rising && !isOver);
            if (!isActive && willBeActive) {
                threshold.callback();
            }
            threshold.active = willBeActive;
        });
    }
    _startThresholdWatcher() {
        if (this.thresholdSub) {
            return;
        }
        this.thresholdSub = subscribeInterval(() => {
            this._executeThresholds();
        }, 32);
    }
    onEdge(type, value, callback) {
        this.thresholds.push({
            value,
            rising: type === 'rising',
            callback,
            active: false,
        });
        this._startThresholdWatcher();
    }
};
__decorate([
    component(MicrophoneComponent)
], MicrophonePart.prototype, "core", void 0);
MicrophonePart = __decorate([
    part('microphone')
], MicrophonePart);
export { MicrophonePart };
//# sourceMappingURL=microphone.js.map