import { Associates } from "@intelligentgraphics/ig.gfx.planning";
import { isNullOrUndefined } from "@intelligentgraphics/ig.utilities";
import { placeComponentAtAttachPoint } from "./Util";
export const createAPIUtil = (session, api) => {
    const getRootGfxId = (comId) => {
        const gfxId = session.getArticleGfxId(comId);
        if (gfxId === undefined) {
            return undefined;
        }
        return gfxId;
    };
    const getScenePosition = (comId) => {
        const position = api.GetPosition(getRootGfxId(comId));
        return position;
    };
    const placeArticleAtAttachPoint = (parentComId, childComId, parentAttachPoint, childAttachPoint) => {
        const parentGfxId = getRootGfxId(parentComId);
        const childGfxId = getRootGfxId(childComId);
        return placeComponentAtAttachPoint(api, parentGfxId, childGfxId, parentAttachPoint, childAttachPoint);
    };
    const getDirectParent = (comId) => {
        const gfxId = getRootGfxId(comId);
        if (gfxId === undefined) {
            return undefined;
        }
        const result = Associates.getAttachPointAtParent(gfxId);
        if (isNullOrUndefined(result)) {
            return undefined;
        }
        const article = session.getComIdByGfxId(result.otherId || result["parentId"]);
        if (article === undefined) {
            return undefined;
        }
        // TODO: remove bracket access to unknown properties once igx >= 3.0.0 is used everywhere
        return {
            foreignComId: article,
            foreignPointKey: result.otherPointKey || result["parentPointKey"],
            ownPointKey: result.ownPointKey,
            type: "family",
        };
    };
    const getDirectChildren = (comId) => {
        const gfxId = getRootGfxId(comId);
        if (gfxId === undefined) {
            return [];
        }
        const result = [];
        const children = Associates.getChildren([gfxId], false);
        for (const child of children) {
            const comId = session.getComIdByGfxId(child);
            if (comId !== undefined) {
                result.push(comId);
            }
        }
        return result;
    };
    const getNeighbors = (comId) => {
        const gfxId = getRootGfxId(comId);
        if (gfxId === undefined) {
            return [];
        }
        const result = [];
        const neighborPoints = Associates.getAttachPointsAtNeighbors(gfxId);
        for (const point of neighborPoints) {
            const foreignComId = session.getComIdByGfxId(point.otherId);
            if (foreignComId !== undefined) {
                result.push({
                    foreignComId,
                    foreignPointKey: point.otherPointKey,
                    ownPointKey: point.ownPointKey,
                    type: "neighbor",
                });
            }
        }
        return result;
    };
    const getPointByKey = (comId, key) => {
        const gfxId = getRootGfxId(comId);
        if (gfxId === undefined) {
            return undefined;
        }
        const points = api.GetPoints(gfxId);
        return points.find(point => point.key === key);
    };
    return {
        getRootGfxId,
        getScenePosition,
        placeArticleAtAttachPoint,
        getDirectChildren,
        getDirectParent,
        getNeighbors,
        getPointByKey,
    };
};
