import { getContext, setContext } from 'svelte';
const key = Symbol('origin-context');
export const provideOrigin = () => {
    const position = $state([0, 0, 0]);
    let rotation = $state(0);
    let commitFn = () => { };
    const context = {
        get position() {
            return position;
        },
        get rotation() {
            return rotation;
        },
        set(pos, rot) {
            if (pos) {
                position[0] = pos[0];
                position[1] = pos[1];
                position[2] = pos[2];
            }
            if (rot !== undefined) {
                rotation = rot;
            }
        },
        commit() {
            commitFn();
        },
        registerCommit(fn) {
            commitFn = fn;
        },
        toZUpPos(out, p) {
            const cos = Math.cos(rotation);
            const sin = Math.sin(rotation);
            out.set(position[0] + cos * p.x - sin * p.y, position[1] + sin * p.x + cos * p.y, position[2] + p.z);
            return out;
        },
        toZUpDir(v) {
            const cos = Math.cos(rotation);
            const sin = Math.sin(rotation);
            const x = v.x;
            v.x = cos * x - sin * v.y;
            v.y = sin * x + cos * v.y;
            return v;
        },
    };
    setContext(key, context);
    return context;
};
export const useOrigin = () => {
    return getContext(key);
};
