import { AudioFileBox, TransientMarkerBox } from "@opendaw/studio-boxes";
import { RuntimeNotifier, UUID } from "@opendaw/lib-std";
export var AudioFileBoxFactory;
(function (AudioFileBoxFactory) {
    const applyTransients = (audioFileBox, transients) => transients.forEach(position => TransientMarkerBox.create(audioFileBox.graph, UUID.generate(), box => {
        box.owner.refer(audioFileBox.transientMarkers);
        box.position.setValue(position);
    }));
    AudioFileBoxFactory.createModifier = async (transientProtocol, boxGraph, audioData, uuid, name) => {
        const optAudioFileBox = boxGraph.findBox(uuid);
        if (optAudioFileBox.nonEmpty()) {
            const audioFileBox = optAudioFileBox.unwrap();
            if (audioFileBox.transientMarkers.pointerHub.isEmpty()) {
                const handler = RuntimeNotifier.progress({ headline: "Detecting Transients..." });
                const transients = await transientProtocol.detect(audioData);
                handler.terminate();
                return () => {
                    if (audioFileBox.transientMarkers.pointerHub.isEmpty()) {
                        applyTransients(audioFileBox, transients);
                    }
                    return audioFileBox;
                };
            }
            return () => audioFileBox;
        }
        else {
            const transients = await transientProtocol.detect(audioData);
            const durationInSeconds = audioData.numberOfFrames / audioData.sampleRate;
            return () => {
                // Re-check in case another drop created it between createModifier and now
                const existingBox = boxGraph.findBox(uuid);
                if (existingBox.nonEmpty()) {
                    const box = existingBox.unwrap();
                    if (box.transientMarkers.pointerHub.isEmpty()) {
                        applyTransients(box, transients);
                    }
                    return box;
                }
                const audioFileBox = AudioFileBox.create(boxGraph, uuid, box => {
                    box.fileName.setValue(name);
                    box.startInSeconds.setValue(0);
                    box.endInSeconds.setValue(durationInSeconds);
                });
                applyTransients(audioFileBox, transients);
                return audioFileBox;
            };
        }
    };
})(AudioFileBoxFactory || (AudioFileBoxFactory = {}));
