import { hierarchy, traits } from './ecs';
export class FrameConfigUpdater {
    updateFrame;
    removeFrame;
    constructor(updateFrame, removeFrame) {
        this.updateFrame = updateFrame;
        this.removeFrame = removeFrame;
    }
    updateLocalPosition = (entity, position) => {
        const { x, y, z } = position;
        if (x === undefined && y === undefined && z === undefined)
            return;
        const change = {};
        if (x !== undefined)
            change.x = x;
        if (y !== undefined)
            change.y = y;
        if (z !== undefined)
            change.z = z;
        entity.set(traits.EditedPose, change);
        const name = entity.get(traits.Name);
        const parent = hierarchy.getParentName(entity) ?? 'world';
        const updatedPose = entity.get(traits.EditedPose);
        if (name && updatedPose) {
            this.updateFrame(name, parent, updatedPose);
        }
    };
    updateLocalOrientation = (entity, orientation) => {
        const { oX, oY, oZ, theta } = orientation;
        if (oX === undefined && oY === undefined && oZ === undefined && theta === undefined) {
            return;
        }
        const change = {};
        if (oX !== undefined)
            change.oX = oX;
        if (oY !== undefined)
            change.oY = oY;
        if (oZ !== undefined)
            change.oZ = oZ;
        if (theta !== undefined)
            change.theta = theta;
        entity.set(traits.EditedPose, change);
        const name = entity.get(traits.Name);
        const parent = hierarchy.getParentName(entity) ?? 'world';
        const updatedPose = entity.get(traits.EditedPose);
        if (name && updatedPose) {
            this.updateFrame(name, parent, updatedPose);
        }
    };
    updateGeometry = (entity, geometry) => {
        const name = entity.get(traits.Name);
        const parent = hierarchy.getParentName(entity) ?? 'world';
        const pose = entity.get(traits.EditedPose);
        if (geometry?.type === 'box') {
            const { x, y, z } = geometry;
            if (x === undefined && y === undefined && z === undefined)
                return;
            const change = {};
            if (x !== undefined)
                change.x = x;
            if (y !== undefined)
                change.y = y;
            if (z !== undefined)
                change.z = z;
            entity.set(traits.Box, change);
            const box = entity.get(traits.Box);
            if (name && box && pose) {
                this.updateFrame(name, parent, pose, { type: 'box', ...box });
            }
        }
        else if (geometry?.type === 'sphere') {
            const { r } = geometry;
            if (r === undefined)
                return;
            entity.set(traits.Sphere, { r });
            const sphere = entity.get(traits.Sphere);
            if (name && sphere && pose) {
                this.updateFrame(name, parent, pose, { type: 'sphere', ...sphere });
            }
        }
        else if (geometry?.type === 'capsule') {
            const { r, l } = geometry;
            if (r === undefined && l === undefined)
                return;
            const change = {};
            if (r !== undefined)
                change.r = r;
            if (l !== undefined)
                change.l = l;
            entity.set(traits.Capsule, change);
            const capsule = entity.get(traits.Capsule);
            if (name && capsule && pose) {
                this.updateFrame(name, parent, pose, { type: 'capsule', ...capsule });
            }
        }
    };
    setFrameParent = (entity, parentName) => {
        const name = entity.get(traits.Name);
        const pose = entity.get(traits.EditedPose);
        if (name && pose) {
            this.updateFrame(name, parentName, pose);
        }
    };
    deleteFrame = (entity) => {
        const name = entity.get(traits.Name);
        if (name) {
            this.removeFrame(name);
        }
    };
    setGeometryType = (entity, type) => {
        const name = entity.get(traits.Name);
        const parent = hierarchy.getParentName(entity) ?? 'world';
        const pose = entity.get(traits.EditedPose);
        if (!name || !pose)
            return;
        if (type === 'none') {
            this.updateFrame(name, parent, pose, { type: 'none' });
        }
        else if (type === 'box') {
            this.updateFrame(name, parent, pose, { type: 'box', x: 100, y: 100, z: 100 });
        }
        else if (type === 'sphere') {
            this.updateFrame(name, parent, pose, { type: 'sphere', r: 100 });
        }
        else if (type === 'capsule') {
            this.updateFrame(name, parent, pose, { type: 'capsule', r: 20, l: 100 });
        }
    };
}
