import { Transform, Shape, Actor } from 'dacha';
import { getIdByPath } from '../../../utils/get-id-by-path';
import { SCENE_PATH_LEGTH, FRAME_STROKE_WIDTH, AREA_STROKE_WIDTH, } from './consts';
const accumulatePath = (actor, path) => {
    path.unshift(`id:${actor.id}`);
    if (actor.parent instanceof Actor) {
        path.unshift('children');
        accumulatePath(actor.parent, path);
    }
};
export const buildActorPath = (actor, sceneId) => {
    const path = [];
    accumulatePath(actor, path);
    path.unshift('scenes', `id:${sceneId}`, 'actors');
    return path;
};
export const updateFrameSize = (frame, actor, bounds, zoom) => {
    const frameTransform = frame.getComponent(Transform);
    const frameShape = frame.getComponent(Shape);
    const actorTransform = actor.getComponent(Transform);
    frameTransform.world.position.x = actorTransform.world.position.x;
    frameTransform.world.position.y = actorTransform.world.position.y;
    frameShape.width = bounds.width;
    frameShape.height = bounds.height;
    frameShape.strokeWidth = FRAME_STROKE_WIDTH / zoom;
};
export const updateAreaSize = (selectionArea, zoom) => {
    const { sceneSize, area } = selectionArea;
    const transform = area.getComponent(Transform);
    const shape = area.getComponent(Shape);
    transform.world.position.x = (sceneSize.x0 + sceneSize.x1) / 2;
    transform.world.position.y = (sceneSize.y0 + sceneSize.y1) / 2;
    shape.width = Math.abs(sceneSize.x0 - sceneSize.x1);
    shape.height = Math.abs(sceneSize.y0 - sceneSize.y1);
    shape.strokeWidth = AREA_STROKE_WIDTH / zoom;
};
export const getActorIdByPath = (path, currentSceneId) => {
    if (!currentSceneId) {
        return undefined;
    }
    if (path !== undefined &&
        path[0] === 'scenes' &&
        path.length > SCENE_PATH_LEGTH) {
        const sceneId = getIdByPath([path[1]]);
        if (sceneId !== currentSceneId) {
            return undefined;
        }
        return getIdByPath(path);
    }
    return undefined;
};
