"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var ig_gfx_planning_1 = require("@intelligentgraphics/ig.gfx.planning");
var ig_utilities_1 = require("@intelligentgraphics/ig.utilities");
var Util_1 = require("./Util");
exports.createAPIUtil = function (session, api) {
    var getRootGfxId = function (comId) {
        var gfxId = session.getArticleGfxId(comId);
        if (gfxId === undefined) {
            return undefined;
        }
        return gfxId;
    };
    var getScenePosition = function (comId) {
        var position = api.GetPosition(getRootGfxId(comId));
        return position;
    };
    var placeArticleAtAttachPoint = function (parentComId, childComId, parentAttachPoint, childAttachPoint) {
        var parentGfxId = getRootGfxId(parentComId);
        var childGfxId = getRootGfxId(childComId);
        return Util_1.placeComponentAtAttachPoint(api, parentGfxId, childGfxId, parentAttachPoint, childAttachPoint);
    };
    var getDirectParent = function (comId) {
        var gfxId = getRootGfxId(comId);
        if (gfxId === undefined) {
            return undefined;
        }
        var result = ig_gfx_planning_1.Associates.getAttachPointAtParent(gfxId);
        if (ig_utilities_1.isNullOrUndefined(result)) {
            return undefined;
        }
        var 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",
        };
    };
    var getDirectChildren = function (comId) {
        var e_1, _a;
        var gfxId = getRootGfxId(comId);
        if (gfxId === undefined) {
            return [];
        }
        var result = [];
        var children = ig_gfx_planning_1.Associates.getChildren([gfxId], false);
        try {
            for (var children_1 = tslib_1.__values(children), children_1_1 = children_1.next(); !children_1_1.done; children_1_1 = children_1.next()) {
                var child = children_1_1.value;
                var comId_1 = session.getComIdByGfxId(child);
                if (comId_1 !== undefined) {
                    result.push(comId_1);
                }
            }
        }
        catch (e_1_1) { e_1 = { error: e_1_1 }; }
        finally {
            try {
                if (children_1_1 && !children_1_1.done && (_a = children_1.return)) _a.call(children_1);
            }
            finally { if (e_1) throw e_1.error; }
        }
        return result;
    };
    var getNeighbors = function (comId) {
        var e_2, _a;
        var gfxId = getRootGfxId(comId);
        if (gfxId === undefined) {
            return [];
        }
        var result = [];
        var neighborPoints = ig_gfx_planning_1.Associates.getAttachPointsAtNeighbors(gfxId);
        try {
            for (var neighborPoints_1 = tslib_1.__values(neighborPoints), neighborPoints_1_1 = neighborPoints_1.next(); !neighborPoints_1_1.done; neighborPoints_1_1 = neighborPoints_1.next()) {
                var point = neighborPoints_1_1.value;
                var foreignComId = session.getComIdByGfxId(point.otherId);
                if (foreignComId !== undefined) {
                    result.push({
                        foreignComId: foreignComId,
                        foreignPointKey: point.otherPointKey,
                        ownPointKey: point.ownPointKey,
                        type: "neighbor",
                    });
                }
            }
        }
        catch (e_2_1) { e_2 = { error: e_2_1 }; }
        finally {
            try {
                if (neighborPoints_1_1 && !neighborPoints_1_1.done && (_a = neighborPoints_1.return)) _a.call(neighborPoints_1);
            }
            finally { if (e_2) throw e_2.error; }
        }
        return result;
    };
    var getPointByKey = function (comId, key) {
        var gfxId = getRootGfxId(comId);
        if (gfxId === undefined) {
            return undefined;
        }
        var points = api.GetPoints(gfxId);
        return points.find(function (point) { return point.key === key; });
    };
    return {
        getRootGfxId: getRootGfxId,
        getScenePosition: getScenePosition,
        placeArticleAtAttachPoint: placeArticleAtAttachPoint,
        getDirectChildren: getDirectChildren,
        getDirectParent: getDirectParent,
        getNeighbors: getNeighbors,
        getPointByKey: getPointByKey,
    };
};
