import * as pc from "playcanvas";
import { Debugger, ViverseApp } from './toolkit/core.mjs';
import { VideoTexture } from './toolkit/extension.mjs';
/** @enum {string} */
const VideoSourceType = {
    Asset: 'asset',
    URL: 'url',
};
/** @enum {string} */
const ProjectionType = {
    Flat: 'flat',
    Spherical: 'spherical',
};
/** @enum {string} */
const RenderSideType = {
    Front: 'front',
    Back: 'back',
    Double: 'double',
};
export class ViverseVideoTexture extends pc.Script {
    static scriptName = 'viverseVideoTexture';
    videoTexture = null;
    /**
     * The source of the video texture.
     * @attribute
     * @title Video Source
     * @type {VideoSourceType}
     */
    videoSource = VideoSourceType.Asset;
    /**
     * The video asset to be used as the texture source.
     * @attribute
     * @title Asset
     * @type {pc.Asset}
     * @resource audio
     * @visibleif {videoSource === 'asset'}
     */
    asset;
    /**
     * The URL of the video to be used as the texture source.
     * Please ensure the URL is accessible and not blocked by CORS or other access restrictions.
     * @attribute
     * @title URL
     * @type {string}
     * @visibleif {videoSource === 'url'}
     */
    url;
    /**
     * The projection type of the video texture.
     * @attribute
     * @title Projection
     * @type {ProjectionType}
     */
    projection = ProjectionType.Flat;
    /**
     * The render side of the video texture.
     * @attribute
     * @title Render Side
     * @type {RenderSideType}
     */
    renderSide = RenderSideType.Double;
    /**
     * Whether the video should autoplay
     * @attribute
     * @title Auto Play
     * @type {boolean}
     */
    autoPlay = true;
    /**
     * Whether the video should loop
     * @attribute
     * @title Loop
     * @type {boolean}
     */
    loop = true;
    /**
     * Whether the video is muted
     * @attribute
     * @title Muted
     * @type {boolean}
     */
    muted = true;
    /**
     * Whether the sound of the video is positional
     * @attribute
     * @title Positional
     * @type {boolean}
     */
    positional = true;
    /**
     * Default volume of the video texture
     * @attribute
     * @title Volume
     * @type {number}
     * @range [0, 1]
     * @precision 2
     * @step 0.01
     */
    volume = 1;
    initialize() {
        const viverseApp = ViverseApp.getApplication();
        if (!viverseApp) {
            Debugger.error('[ViverseVideoTexture] ViverseApp is not initialized.');
            return;
        }
        let src = '';
        if (this.videoSource === VideoSourceType.Asset) {
            if (!this.asset) {
                Debugger.error('[ViverseVideoTexture] Video asset is not set.');
                return;
            }
            const assetUrl = this.asset.getFileUrl() ?? '';
            // @ts-expect-error - window.editor is only available in the editor
            src = window.editor?.api ? this.getEditorUrl(assetUrl) : assetUrl;
        }
        else if (this.videoSource === VideoSourceType.URL) {
            if (!this.url) {
                Debugger.error('[ViverseVideoTexture] Video URL is not set.');
                return;
            }
            src = this.url;
        }
        const pcApp = viverseApp.pcApp;
        this.videoTexture = new VideoTexture(pcApp, this.entity, src, {
            projection: this.projection,
            renderSide: this.renderSide,
            autoPlay: this.autoPlay,
            loop: this.loop,
            muted: this.muted,
            positional: this.positional,
            volume: this.volume,
        });
        this.fire('texture:ready', this.videoTexture);
        this.on('destroy', this.destroy, this);
    }
    getEditorUrl(assetUrl) {
        // Hack: getFileUrl() contains extra "/api/" in Launch mode
        return assetUrl.startsWith('/api/') ? assetUrl.slice(5) : assetUrl;
    }
    destroy() {
        this.videoTexture?.destroy();
        this.videoTexture = null;
    }
}
