"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.layoutVennData = exports.getColorMap = void 0;
var tslib_1 = require("tslib");
var util_1 = require("@antv/util");
var blend_1 = require("../../utils/color/blend");
var layout_1 = require("./layout/layout");
var diagram_1 = require("./layout/diagram");
var constant_1 = require("./constant");
/**
 * 获取 颜色映射
 * @usage colorMap.get(id) => color
 *
 * @returns Map<string, string>
 */
exports.getColorMap = util_1.memoize(function (colorPalette, data, options) {
    var blendMode = options.blendMode, setsField = options.setsField;
    var colorMap = new Map();
    var colorPaletteLen = colorPalette.length;
    data.forEach(function (d, idx) {
        if (d[setsField].length === 1) {
            colorMap.set(d[constant_1.ID_FIELD], colorPalette[(idx + colorPaletteLen) % colorPaletteLen]);
        }
        else {
            /** 一般都是可以获取到颜色的，如果不正确 就是输入了非法数据 */
            var colorArr = d[setsField].map(function (id) { return colorMap.get(id); });
            colorMap.set(d[constant_1.ID_FIELD], colorArr.slice(1).reduce(function (a, b) { return blend_1.blend(a, b, blendMode); }, colorArr[0]));
        }
    });
    return colorMap;
}, function () {
    var params = [];
    for (var _i = 0; _i < arguments.length; _i++) {
        params[_i] = arguments[_i];
    }
    return JSON.stringify(params);
});
/**
 * 给韦恩图数据进行布局
 *
 * @param data
 * @param width
 * @param height
 * @param padding
 * @returns 韦恩图数据
 */
function layoutVennData(options, width, height, padding) {
    if (padding === void 0) { padding = 0; }
    var data = options.data, setsField = options.setsField, sizeField = options.sizeField;
    var vennData = data.map(function (d) {
        var _a;
        return (tslib_1.__assign(tslib_1.__assign({}, d), (_a = { sets: d[setsField] || [], size: d[sizeField] }, _a[constant_1.PATH_FIELD] = '', _a[constant_1.ID_FIELD] = '', _a)));
    });
    // 1. 进行排序，避免图形元素遮挡
    vennData.sort(function (a, b) { return a.sets.length - b.sets.length; });
    // todo 2. 可以在这里处理下非法数据输入，避免直接 crash
    var solution = layout_1.venn(vennData);
    var circles = layout_1.scaleSolution(solution, width, height, padding);
    var textCenters = diagram_1.computeTextCentres(circles, vennData);
    vennData.forEach(function (row) {
        var sets = row.sets;
        var id = sets.join(',');
        row[constant_1.ID_FIELD] = id;
        if (sets.length === 1) {
            var circle = circles[id];
            row[constant_1.PATH_FIELD] = diagram_1.circlePath(circle.x, circle.y, circle.radius);
            util_1.assign(row, circle);
        }
        else {
            var setCircles = sets.map(function (set) { return circles[set]; });
            var path = diagram_1.intersectionAreaPath(setCircles);
            if (!/[zZ]$/.test(path)) {
                path += ' Z';
            }
            row[constant_1.PATH_FIELD] = path;
            var center = textCenters[id] || { x: 0, y: 0 };
            util_1.assign(row, center);
        }
    });
    return vennData;
}
exports.layoutVennData = layoutVennData;
//# sourceMappingURL=utils.js.map