import { traits } from './ecs';
export class FrameConfigUpdater {
    updateFrame;
    removeFrame;
    constructor(updateFrame, removeFrame) {
        this.updateFrame = updateFrame;
        this.removeFrame = removeFrame;
    }
    updateLocalPosition = (entity, position) => {
        const x = this.sanitizeFloatValue(position.x);
        const y = this.sanitizeFloatValue(position.y);
        const z = this.sanitizeFloatValue(position.z);
        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 = entity.get(traits.Parent) ?? 'world';
        const updatedPose = entity.get(traits.EditedPose);
        if (name && updatedPose) {
            this.updateFrame(name, parent, updatedPose);
        }
    };
    updateLocalOrientation = (entity, orientation) => {
        const oX = this.sanitizeFloatValue(orientation.oX);
        const oY = this.sanitizeFloatValue(orientation.oY);
        const oZ = this.sanitizeFloatValue(orientation.oZ);
        const theta = this.sanitizeFloatValue(orientation.theta);
        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 = entity.get(traits.Parent) ?? '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 = entity.get(traits.Parent) ?? 'world';
        const pose = entity.get(traits.EditedPose);
        if (geometry?.type === 'box') {
            const x = this.sanitizeFloatValue(geometry.x);
            const y = this.sanitizeFloatValue(geometry.y);
            const z = this.sanitizeFloatValue(geometry.z);
            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 = this.sanitizeFloatValue(geometry.r);
            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 = this.sanitizeFloatValue(geometry.r);
            const l = this.sanitizeFloatValue(geometry.l);
            if (r === undefined && l === undefined)
                return;
            const change = {};
            if (r !== undefined)
                change.r = r;
            if (l !== undefined)
                change.l = l;
            entity.set(traits.Capsule, { r, l });
            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 = entity.get(traits.Parent) ?? '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 });
        }
    };
    sanitizeFloatValue = (value) => {
        if (value === undefined) {
            return undefined;
        }
        const num = Number.parseFloat(value.toFixed(2));
        if (Number.isNaN(num)) {
            return undefined;
        }
        return value;
    };
}
