import { Script, Entity } from "playcanvas";
import { Debugger, ViverseApp } from './toolkit/core.mjs';
import { PickInteraction } from './toolkit/extension.mjs';
/**
 * Internal script attached to cloned pickable entities.
 * This script manages the lifecycle of the PickInteraction instance for cloned entities.
 * It is automatically created by ViversePick and should not be added manually.
 * @internal
 */
class ViversePickInstance extends Script {
    static scriptName = 'viversePickInstance';
    gripPoint;
    _pick = null;
    initialize() {
        const viverseApp = ViverseApp.getApplication();
        if (!viverseApp) {
            Debugger.error('[ViversePickInstance] ViverseApp is not initialized.');
            return;
        }
        const config = {
            name: `${this.entity.name}_${crypto.randomUUID()}`,
            gripPoint: this.gripPoint,
        };
        this._pick = new PickInteraction(viverseApp, this.entity, config);
        this.on('destroy', this.destroy, this);
    }
    destroy() {
        if (this._pick) {
            this._pick.destroy();
            this._pick = null;
        }
    }
}
/**
 * ViversePick script for creating pickable entities.
 * On initialization, it clones the entity and creates an independent pickable instance.
 *
 * @remarks
 * - Source entity: Usually be in the scene, serves as a template for cloning to avoid change scene side effects
 * - Cloned entity: Created on initialization, has independent lifecycle managed by ViversePickInstance
 */
export class ViversePick extends Script {
    static scriptName = 'viversePick';
    clonedEntity = null;
    /**
     * The grip point entity where the item will be attached.
     * Needs to be a child of the pickable item entity.
     * @attribute
     * @title Grip Point
     * @type {Entity}
     */
    gripPoint;
    initialize() {
        // Clone entity to create an independent pickable instance
        const { entity, gripPoint } = this._cloneEntityFromScene();
        // Attach ViversePickInstance to manage the cloned entity's lifecycle
        this.clonedEntity = entity;
        entity.addComponent('script');
        entity.script.create(ViversePickInstance, {
            properties: {
                gripPoint: gripPoint,
            },
        });
    }
    getPickHandler() {
        const viverseApp = ViverseApp.getApplication();
        if (!viverseApp)
            return null;
        const localPlayerSystem = viverseApp.systems.localPlayer;
        if (!localPlayerSystem)
            return null;
        const interactionModule = localPlayerSystem.modules?.interaction;
        if (!interactionModule)
            return null;
        return interactionModule.pickHandler;
    }
    _cloneEntityFromScene() {
        const pickHandler = this.getPickHandler();
        if (!pickHandler) {
            throw new Error('PickInteractionHandler is not available');
        }
        const onBeforeAddToScene = (clonedEntity) => {
            // Remove script from cloned entity to prevent re-cloning
            clonedEntity.script?.destroy(ViversePick.scriptName);
        };
        const result = pickHandler.cloneEntityFromScene(this.entity, this.gripPoint, onBeforeAddToScene);
        return { entity: result.itemEntity, gripPoint: result.gripPoint };
    }
}
// HACK: This block is purely for ensuring type-only imports are preserved in the build output.
// The attribute-parser needs these imports to correctly parse the script attributes.
// This code will never run and will be tree-shaken from the final application bundle.
if (
// eslint-disable-next-line no-constant-condition
false) {
    Debugger.log(Script, Entity);
}
