"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var igd_core_1 = require("@intelligentgraphics/igd.core");
exports.resolveInitialConfiguration = function (params, articleQuery, selection) {
    var headItem = articleQuery.getHeadItem(params.source);
    if (headItem !== undefined) {
        return joinConfigurations(params.initialConfiguration, headItem.comId, articleQuery);
    }
    if (!params.applySceneConfiguration) {
        return params.initialConfiguration;
    }
    var cloneParent = findSimilarArticle(articleQuery, params.articleNumber.baseArticleNumber, params.source, selection, params.articleNumber.attachTo !== undefined
        ? params.articleNumber.attachTo.comId
        : undefined);
    if (cloneParent === undefined) {
        return params.initialConfiguration;
    }
    if (params.initialConfiguration === undefined) {
        return cloneParent.comId;
    }
    var initialConfiguration = typeof params.initialConfiguration === "string"
        ? articleQuery.getArticleConfiguration(params.initialConfiguration)
        : params.initialConfiguration;
    return joinConfigurations(initialConfiguration, cloneParent.configuration, articleQuery);
};
var findSimilarArticle = function (articleQuery, articleId, sourceId, selection, attachTargetComId) {
    if (selection === void 0) { selection = []; }
    if (attachTargetComId !== undefined) {
        return articleQuery.getArticleByComId(attachTargetComId);
    }
    var selectionArticles = articleQuery.getArticlesByComIds(selection);
    // first: try to find the same article number in selection
    var sameFromSelection = selectionArticles.find(function (article) {
        return article.source === sourceId && article.articleId === articleId;
    });
    if (sameFromSelection) {
        return sameFromSelection;
    }
    // second: try to find the same series in selection
    var sameSeriesFromSelection = selectionArticles.find(function (article) { return article.source === sourceId; });
    if (sameSeriesFromSelection) {
        return sameSeriesFromSelection;
    }
    // third: find the same article in planning
    var sameSeriesArticles = articleQuery.getArticlesOfSource(sourceId);
    var sameInScene = sameSeriesArticles.find(function (article) { return article.articleId === articleId; });
    if (sameInScene) {
        return sameInScene;
    }
    // forth: take the first article of the same series
    if (sameSeriesArticles) {
        return sameSeriesArticles[0];
    }
    return undefined;
};
var joinConfigurations = function (initialConfiguration, otherConfiguration, articleQuery) {
    if (initialConfiguration === undefined) {
        return otherConfiguration;
    }
    var otherConfigurationObject = typeof otherConfiguration === "string"
        ? articleQuery.getArticleConfiguration(otherConfiguration)
        : otherConfiguration;
    var initialConfigurationObject = typeof initialConfiguration === "string"
        ? articleQuery.getArticleConfiguration(initialConfiguration)
        : initialConfiguration;
    return igd_core_1.IGXE.mergePropertyValueChanges(otherConfigurationObject, initialConfigurationObject);
};
