import * as pc from "playcanvas";
import { Debugger, ViverseApp } from './toolkit/core.mjs';
import { SoundAudioZone, VideoTextureAudioZone, } from './toolkit/extension.mjs';
/** @enum {string} */
const SourceType = {
    SoundComponent: 'soundComponent',
    VideoTexture: 'videoTexture',
};
export class ViverseAudioZone extends pc.Script {
    static scriptName = 'viverseAudioZone';
    audioZone = null;
    /**
     * The source type for the audio zone.
     * SoundComponent – audio from a sound component, ensure the ‘sound’ component is added;
     * VideoTexture – audio from a video texture, ensure the ‘viverse-video-texture’ script is added.
     * @attribute
     * @title Source
     * @type {SourceType}
     */
    source = SourceType.VideoTexture;
    /**
     * Scale of the audio zone relative to the root entity's scale
     * @attribute
     * @title Audio Zone Scale
     * @type {pc.Vec3}
     */
    scale = new pc.Vec3(5, 5, 5);
    initialize() {
        const viverseApp = ViverseApp.getApplication();
        if (!viverseApp) {
            Debugger.error('[ViverseAudioZone] ViverseApp is not initialized.');
            return;
        }
        if (this.source === SourceType.SoundComponent) {
            this._createSoundAudioZone(viverseApp);
        }
        else if (this.source === SourceType.VideoTexture) {
            this._createVideoTextureAudioZone(viverseApp);
        }
        else {
            Debugger.error(`[ViverseAudioZone] Unsupported source type: ${this.source}`);
        }
        this.on('destroy', this.destroy, this);
    }
    _createSoundAudioZone(viverseApp) {
        const sound = this.entity.sound;
        if (!sound) {
            Debugger.error(`[ViverseAudioZone] Sound component is not found on the entity ${this.entity.name}.`);
            return;
        }
        this.audioZone = new SoundAudioZone(viverseApp, sound, {
            scale: this.scale,
        });
    }
    _createVideoTextureAudioZone(viverseApp) {
        const script = this.entity.script?.get('viverseVideoTexture');
        if (!script) {
            Debugger.error(`[ViverseAudioZone] viverse-video-texture is not found on the entity ${this.entity.name}.`);
            return;
        }
        const create = (videoTexture) => {
            this.audioZone = new VideoTextureAudioZone(viverseApp, videoTexture);
        };
        if (script.videoTexture) {
            create(script.videoTexture);
        }
        else {
            script.once('texture:ready', create);
        }
        script.once('destroy', this.destroy, this);
    }
    destroy() {
        this.audioZone?.destroy();
        this.audioZone = null;
    }
}
