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;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
// Singleton - one clock worker shared across engine instances
import { Lazy } from "@opendaw/lib-std";
let instance;
export class HRClockWorker {
    static get() { return new HRClockWorker(); }
    sab;
    #worker;
    constructor() {
        // Layout (32 bytes):
        // int32[0]: request counter
        // int32[1]: start response counter (which request this start timestamp is for)
        // int32[2]: end response counter (which request this end timestamp is for)
        // float64[2]: start timestamp (bytes 16-23)
        // float64[3]: end timestamp (bytes 24-31)
        this.sab = new SharedArrayBuffer(32);
        const code = `
            onmessage = (e) => {
                const int32 = new Int32Array(e.data)
                const float64 = new Float64Array(e.data)
                let lastCounter = 0
                while (true) {
                    Atomics.wait(int32, 0, lastCounter)
                    lastCounter = Atomics.load(int32, 0)
                    const isStart = (lastCounter & 1) === 1
                    if (isStart) {
                        float64[2] = performance.now()
                        Atomics.store(int32, 1, lastCounter)
                    } else {
                        float64[3] = performance.now()
                        Atomics.store(int32, 2, lastCounter)
                    }
                }
            }
        `;
        const blob = new Blob([code], { type: "application/javascript" });
        this.#worker = new Worker(URL.createObjectURL(blob));
        this.#worker.postMessage(this.sab);
    }
}
__decorate([
    Lazy,
    __metadata("design:type", Function),
    __metadata("design:paramtypes", []),
    __metadata("design:returntype", HRClockWorker)
], HRClockWorker, "get", null);
