import { isArray, get } from '@antv/util';
import { interaction, animation, theme, tooltip, scale } from '../../adaptor/common';
import { schema as schemaGeometry } from '../../adaptor/geometries';
import { deepAssign, flow, getAdjustAppendPadding, normalPadding, resolveAllPadding } from '../../utils';
import { getColorMap, layoutVennData } from './utils';
import { ID_FIELD } from './constant';
import './shape';
/** 图例默认预留空间 */
export var LEGEND_SPACE = 40;
/**
 * color options 转换
 */
function transformColor(params, data) {
    var chart = params.chart, options = params.options;
    var color = options.color, setsField = options.setsField;
    if (typeof color !== 'function') {
        var colorPalette_1 = typeof color === 'string' ? [color] : color;
        if (!isArray(colorPalette_1)) {
            var _a = chart.getTheme(), colors10 = _a.colors10, colors20 = _a.colors20;
            colorPalette_1 = data.filter(function (d) { return d[setsField].length === 1; }).length <= 10 ? colors10 : colors20;
        }
        var colorMap_1 = getColorMap(colorPalette_1, data, options);
        return function (datum) { return colorMap_1.get(datum[ID_FIELD]) || colorPalette_1[0]; };
    }
    return color;
}
/**
 * 处理 padding
 */
function padding(params) {
    var chart = params.chart, options = params.options;
    var legend = options.legend, appendPadding = options.appendPadding, padding = options.padding;
    // 处理 legend 的位置. 默认预留 40px, 业务上可以通过 appendPadding 增加
    var tempPadding = normalPadding(appendPadding);
    if (legend !== false) {
        tempPadding = getAdjustAppendPadding(appendPadding, get(legend, 'position'), LEGEND_SPACE);
    }
    chart.appendPadding = resolveAllPadding([tempPadding, padding]);
    return params;
}
/**
 * geometry 处理
 * @param params
 */
function geometry(params) {
    var chart = params.chart, options = params.options;
    var pointStyle = options.pointStyle, label = options.label, setsField = options.setsField, sizeField = options.sizeField;
    // 获取容器大小
    var _a = normalPadding(chart.appendPadding), t = _a[0], r = _a[1], b = _a[2], l = _a[3];
    // 处理 legend 的位置. 默认预留 40px, 业务上可以通过 appendPadding 增加
    var customInfo = { offsetX: l, offsetY: t, label: label };
    // coordinateBBox + appendPadding = viewBBox, 不需要再计算 appendPadding 部分，因此直接使用 viewBBox
    var _b = chart.viewBBox, width = _b.width, height = _b.height;
    var vennData = layoutVennData(options, width - (r + l), height - (t + b), 0);
    chart.data(vennData);
    var ext = schemaGeometry(deepAssign({}, params, {
        options: {
            xField: 'x',
            yField: 'y',
            sizeField: sizeField,
            seriesField: ID_FIELD,
            rawFields: [setsField, sizeField],
            // 不使用 G2 的label，直接在自定义 shape 中实现
            label: false,
            schema: {
                shape: 'venn',
                style: pointStyle,
                color: transformColor(params, vennData),
            },
        },
    })).ext;
    var geometry = ext.geometry;
    geometry.customInfo(customInfo);
    return params;
}
/**
 * legend 配置
 * @param params
 */
export function legend(params) {
    var chart = params.chart, options = params.options;
    var legend = options.legend, sizeField = options.sizeField;
    chart.legend(ID_FIELD, legend);
    // 强制不开启 连续图例
    chart.legend(sizeField, false);
    return params;
}
/**
 * 默认关闭坐标轴
 * @param params
 */
export function axis(params) {
    var chart = params.chart;
    chart.axis(false);
    return params;
}
/**
 * 图适配器
 * @param chart
 * @param options
 */
export function adaptor(params) {
    // flow 的方式处理所有的配置到 G2 API
    return flow(padding, theme, geometry, scale({}), legend, axis, tooltip, interaction, animation
    // ... 其他的 adaptor flow
    )(params);
}
//# sourceMappingURL=adaptor.js.map