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 { EventEmitter } from '@kano/common/index.js';
import { PartComponent } from '../../component.js';
import Sequencer from './tone.js';
class SequencerComponent extends PartComponent {
    constructor() {
        super(...arguments);
        this.step = new EventEmitter();
        this.hit = new EventEmitter();
        this.bpm = 120;
    }
}
__decorate([
    property({ type: EventEmitter, value: new EventEmitter(), noReset: true })
], SequencerComponent.prototype, "step", void 0);
__decorate([
    property({ type: EventEmitter, value: new EventEmitter(), noReset: true })
], SequencerComponent.prototype, "hit", void 0);
__decorate([
    property({ type: Number, value: 120 })
], SequencerComponent.prototype, "bpm", void 0);
let SequencerPart = class SequencerPart extends Part {
    constructor() {
        super();
        this.sequencers = new Map();
        this._onDidStep = new EventEmitter();
        this._onDidStepsChange = new EventEmitter();
        this._onDidChangeSequencers = new EventEmitter();
        this.core = this._components.get('core');
        this.core.onDidInvalidate(() => {
            this.render();
        }, this, this.subscriptions);
    }
    get onDidStep() { return this._onDidStep.event; }
    get onDidStepsChange() { return this._onDidStepsChange.event; }
    get onDidChangeSequencers() { return this._onDidChangeSequencers.event; }
    render() {
        if (!this.core.invalidated) {
            return;
        }
        this.sequencers.forEach((sequencer) => {
            sequencer.setBPM(this.core.bpm);
        });
        this.core.apply();
    }
    onStop() {
        super.onStop();
        this.sequencers.forEach(seq => seq.dispose());
        this.sequencers.clear();
        this._onDidChangeSequencers.fire();
    }
    createSteps(id, steps) {
        // Create a new sequencer
        const seq = new Sequencer(steps.length, this.core.bpm);
        // Update its steps from the sequence store
        seq.setSteps(steps.map(num => !!num));
        // Copy the id locally
        const seqId = id;
        this.sequencers.set(seqId, seq);
        seq.onDidStep((col) => {
            this._onDidStep.fire({ id: seqId, column: col });
        });
        seq.onDidStepsChange(() => {
            this._onDidStepsChange.fire({ id: seqId, steps: seq.steps });
        });
        seq.start();
        this._onDidChangeSequencers.fire();
        return seq;
    }
    set bpm(value) {
        const sanitized = Math.max(1, Math.min(360, value));
        this.core.bpm = sanitized;
        this.core.invalidate();
    }
    get bpm() {
        return this.core.bpm;
    }
    start() {
        this.sequencers.forEach(s => s.start());
    }
    stop() {
        this.sequencers.forEach(s => s.stop());
    }
    shuffle() {
        this.sequencers.forEach(s => s.shuffle());
    }
};
__decorate([
    component(SequencerComponent)
], SequencerPart.prototype, "core", void 0);
SequencerPart = __decorate([
    part('sequencer')
], SequencerPart);
export { SequencerPart };
//# sourceMappingURL=sequencer.js.map