import { __assign, __awaiter, __generator, __values } from "tslib";
import { State as CoreState } from "@intelligentgraphics/ig.gfx.core";
import * as MultiArticle from "@intelligentgraphics/ig.gfx.multiarticle";
import { Associates, NeighborType, WorldLocalConverter, } from "@intelligentgraphics/ig.gfx.planning";
export var findMultiArticleConfiguration = function (session, comId) {
    var article = session.getArticleByComId(comId);
    var multiArticles = session.getMultiArticlesForComId(comId);
    if (!multiArticles) {
        return;
    }
    return getMultiArticleConfiguration(multiArticles, article.articleId, article.articleNumber === undefined
        ? undefined
        : article.articleNumber.baseArticleNumber);
};
export var getMultiArticleConfiguration = function (multiArticles) {
    var e_1, _a;
    var articleIds = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        articleIds[_i - 1] = arguments[_i];
    }
    try {
        for (var articleIds_1 = __values(articleIds), articleIds_1_1 = articleIds_1.next(); !articleIds_1_1.done; articleIds_1_1 = articleIds_1.next()) {
            var articleId = articleIds_1_1.value;
            if (articleId === undefined) {
                continue;
            }
            var configuration = multiArticles.getConfiguration(articleId);
            if (configuration !== undefined &&
                Object.getOwnPropertyNames(configuration).length > 0) {
                return configuration;
            }
        }
    }
    catch (e_1_1) { e_1 = { error: e_1_1 }; }
    finally {
        try {
            if (articleIds_1_1 && !articleIds_1_1.done && (_a = articleIds_1.return)) _a.call(articleIds_1);
        }
        finally { if (e_1) throw e_1.error; }
    }
    return {};
};
export var getMultiArticleActions = function (multiArticles) {
    var e_2, _a;
    var articleIds = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        articleIds[_i - 1] = arguments[_i];
    }
    try {
        for (var articleIds_2 = __values(articleIds), articleIds_2_1 = articleIds_2.next(); !articleIds_2_1.done; articleIds_2_1 = articleIds_2.next()) {
            var articleId = articleIds_2_1.value;
            if (articleId === undefined) {
                continue;
            }
            var actions = multiArticles.getActions(articleId);
            if (actions !== undefined && actions.length > 0) {
                return actions;
            }
        }
    }
    catch (e_2_1) { e_2 = { error: e_2_1 }; }
    finally {
        try {
            if (articleIds_2_1 && !articleIds_2_1.done && (_a = articleIds_2.return)) _a.call(articleIds_2);
        }
        finally { if (e_2) throw e_2.error; }
    }
    return [];
};
export var getMultiArticleCategories = function (multiArticles) {
    var e_3, _a;
    var articleIds = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        articleIds[_i - 1] = arguments[_i];
    }
    try {
        for (var articleIds_3 = __values(articleIds), articleIds_3_1 = articleIds_3.next(); !articleIds_3_1.done; articleIds_3_1 = articleIds_3.next()) {
            var articleId = articleIds_3_1.value;
            if (articleId === undefined) {
                continue;
            }
            var categories = multiArticles.getCategories(articleId);
            if (categories !== undefined &&
                Object.getOwnPropertyNames(categories).length > 0) {
                return categories;
            }
        }
    }
    catch (e_3_1) { e_3 = { error: e_3_1 }; }
    finally {
        try {
            if (articleIds_3_1 && !articleIds_3_1.done && (_a = articleIds_3.return)) _a.call(articleIds_3);
        }
        finally { if (e_3) throw e_3.error; }
    }
    return {};
};
export var getMultiArticleAttachPoints = function (multiArticles) {
    var e_4, _a;
    var articleIds = [];
    for (var _i = 1; _i < arguments.length; _i++) {
        articleIds[_i - 1] = arguments[_i];
    }
    try {
        for (var articleIds_4 = __values(articleIds), articleIds_4_1 = articleIds_4.next(); !articleIds_4_1.done; articleIds_4_1 = articleIds_4.next()) {
            var articleId = articleIds_4_1.value;
            if (articleId === undefined) {
                continue;
            }
            var attachPoints = multiArticles.getAttachPoints(articleId);
            if (attachPoints !== undefined && attachPoints.length > 0) {
                return attachPoints;
            }
        }
    }
    catch (e_4_1) { e_4 = { error: e_4_1 }; }
    finally {
        try {
            if (articleIds_4_1 && !articleIds_4_1.done && (_a = articleIds_4.return)) _a.call(articleIds_4);
        }
        finally { if (e_4) throw e_4.error; }
    }
    return [];
};
export var createMultiArticleHooks = function (session) {
    var generateActionConfiguration = function (actions, comId, context) {
        var multiArticles = session.getMultiArticlesForComId(comId);
        if (!multiArticles) {
            return;
        }
        var _a = session.getArticleByComId(comId), configuration = _a.configuration, gfxId = _a.gfxId, articleId = _a.articleId;
        var hasLeftNeighbor = Boolean(Associates.neighbor(gfxId, NeighborType.Left));
        var hasRightNeighbor = Boolean(Associates.neighbor(gfxId, NeighborType.Right));
        var hasTopNeighbor = Boolean(Associates.neighbor(gfxId, NeighborType.Top));
        var hasBottomNeighbor = Boolean(Associates.neighbor(gfxId, NeighborType.Bottom));
        var converter = new WorldLocalConverter(context);
        var relativeSnapPosition = converter.toLocal(CoreState.API.GetPosition(gfxId));
        var additionalRuleParameters = MultiArticle.Rule.createAdditionalRuleParameters({
            context: context,
            hasLeftNeighbor: hasLeftNeighbor,
            hasRightNeighbor: hasRightNeighbor,
            hasBottomNeighbor: hasBottomNeighbor,
            hasTopNeighbor: hasTopNeighbor,
            relativeSnapPosition: relativeSnapPosition,
        });
        var maConfig = findMultiArticleConfiguration(session, comId);
        var properties = __assign(__assign(__assign({}, configuration), maConfig), additionalRuleParameters);
        var propertyValuePairs = actions.reduce(function (pairs, action) {
            var evaluatedAction = MultiArticle.Rule.evalActionRule(action, properties);
            if (evaluatedAction.isActive &&
                evaluatedAction.property !== undefined &&
                configuration[evaluatedAction.property] !==
                    evaluatedAction.value) {
                pairs[evaluatedAction.property] = evaluatedAction.value;
            }
            return pairs;
        }, {});
        return propertyValuePairs;
    };
    var executeAction = function (action, id, context) { return executeActions([action], id, context); };
    var executeActions = function (actions, gfxId, context) { return __awaiter(void 0, void 0, void 0, function () {
        var item, propertyValuePairs;
        return __generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    item = session.getArticleByGfxId(gfxId);
                    if (item === undefined) {
                        throw new Error("Could not find article instance for gfxId " + gfxId + ". Not executing actions");
                    }
                    propertyValuePairs = generateActionConfiguration(actions, item.comId, context);
                    if (!(propertyValuePairs !== undefined &&
                        Object.getOwnPropertyNames(propertyValuePairs).length > 0)) return [3 /*break*/, 2];
                    return [4 /*yield*/, session.changeArticleProperties(item.comId, propertyValuePairs)];
                case 1:
                    _a.sent();
                    return [2 /*return*/, true];
                case 2: return [2 /*return*/, false];
            }
        });
    }); };
    return {
        generateActionConfiguration: generateActionConfiguration,
        executeAction: executeAction,
        executeActions: executeActions,
    };
};
