import { __assign, __rest } from "tslib";
import { every, filter, isFunction, isString, isNil, get } from '@antv/util';
import { legend, tooltip, interaction, animation, theme, state, annotation } from '../../adaptor/common';
import { interval } from '../../adaptor/geometries';
import { flow, LEVEL, log, template, transformLabel, deepAssign, renderStatistic } from '../../utils';
import { adaptOffset, getTotalValue } from './utils';
/**
 * 字段
 * @param params
 */
function geometry(params) {
    var chart = params.chart, options = params.options;
    var data = options.data, angleField = options.angleField, colorField = options.colorField, color = options.color, pieStyle = options.pieStyle;
    // 处理不合法的数据
    var processData = filter(data, function (d) { return typeof d[angleField] === 'number' || isNil(d[angleField]); });
    // 打印异常数据情况
    log(LEVEL.WARN, processData.length === data.length, 'illegal data existed in chart data.');
    var allZero = every(processData, function (d) { return d[angleField] === 0; });
    if (allZero) {
        // 数据全 0 处理，调整 position 映射
        var percentageField_1 = '$$percentage$$';
        processData = processData.map(function (d) {
            var _a;
            return (__assign(__assign({}, d), (_a = {}, _a[percentageField_1] = 1 / processData.length, _a)));
        });
        chart.data(processData);
        var p = deepAssign({}, params, {
            options: {
                xField: '1',
                yField: percentageField_1,
                seriesField: colorField,
                isStack: true,
                interval: {
                    color: color,
                    style: pieStyle,
                },
            },
        });
        interval(p);
        // all zero 额外处理
        chart.geometries[0].tooltip(colorField + "*" + angleField);
    }
    else {
        chart.data(processData);
        var p = deepAssign({}, params, {
            options: {
                xField: '1',
                yField: angleField,
                seriesField: colorField,
                isStack: true,
                interval: {
                    color: color,
                    style: pieStyle,
                },
            },
        });
        interval(p);
    }
    return params;
}
/**
 * meta 配置
 * @param params
 */
function meta(params) {
    var _a;
    var chart = params.chart, options = params.options;
    var meta = options.meta, colorField = options.colorField;
    // meta 直接是 scale 的信息
    var scales = deepAssign({}, meta);
    chart.scale(scales, (_a = {},
        _a[colorField] = { type: 'cat' },
        _a));
    return params;
}
/**
 * coord 配置
 * @param params
 */
function coordinate(params) {
    var chart = params.chart, options = params.options;
    var radius = options.radius, innerRadius = options.innerRadius;
    chart.coordinate({
        type: 'theta',
        cfg: {
            radius: radius,
            innerRadius: innerRadius,
        },
    });
    return params;
}
/**
 * label 配置
 * @param params
 */
function label(params) {
    var chart = params.chart, options = params.options;
    var label = options.label, colorField = options.colorField, angleField = options.angleField;
    var geometry = chart.geometries[0];
    // label 为 false, 空 则不显示 label
    if (!label) {
        geometry.label(false);
    }
    else {
        var callback = label.callback, cfg = __rest(label, ["callback"]);
        var labelCfg = transformLabel(cfg);
        // ① 提供模板字符串的 label content 配置
        if (labelCfg.content) {
            var content_1 = labelCfg.content;
            labelCfg.content = function (data, dataum, index) {
                var name = data[colorField];
                var value = data[angleField];
                // dymatic get scale, scale is ready this time
                var angleScale = chart.getScaleByField(angleField);
                var percent = angleScale === null || angleScale === void 0 ? void 0 : angleScale.scale(value);
                return isFunction(content_1)
                    ? // append pecent (number) to data, users can get origin data from `dataum._origin`
                        content_1(__assign(__assign({}, data), { percent: percent }), dataum, index)
                    : isString(content_1)
                        ? template(content_1, {
                            value: value,
                            name: name,
                            // percentage (string), default keep 2
                            percentage: percent ? (percent * 100).toFixed(2) + "%" : null,
                        })
                        : content_1;
            };
        }
        var LABEL_LAYOUT_TYPE_MAP = {
            inner: '',
            outer: 'pie-outer',
            spider: 'pie-spider',
        };
        var labelLayoutType = LABEL_LAYOUT_TYPE_MAP[labelCfg.type] || 'pie-outer';
        labelCfg.layout = deepAssign({}, labelCfg.layout, { type: labelLayoutType });
        geometry.label({
            // fix: could not create scale, when field is undefined（attributes 中的 fields 定义都会被用来创建 scale）
            fields: colorField ? [angleField, colorField] : [angleField],
            callback: callback,
            cfg: __assign(__assign({}, labelCfg), { offset: adaptOffset(labelCfg.type, labelCfg.offset), type: 'pie' }),
        });
    }
    return params;
}
/**
 * statistic 中心文本配置
 * @param params
 */
function statistic(params) {
    var chart = params.chart, options = params.options;
    var innerRadius = options.innerRadius, statistic = options.statistic, angleField = options.angleField, colorField = options.colorField, meta = options.meta;
    /** 中心文本 指标卡 */
    if (innerRadius && statistic) {
        var title = statistic.title, content = statistic.content;
        if (title !== false && !get(title, 'formatter')) {
            // @ts-ignore
            title.formatter = function (datum) { return (datum ? datum[colorField] : '总计'); };
        }
        if (content !== false && !get(content, 'formatter')) {
            // @ts-ignore
            content.formatter = function (datum, data) {
                var metaFormatter = get(meta, [angleField, 'formatter']);
                var dataValue = datum ? datum[angleField] : getTotalValue(data, angleField);
                return metaFormatter ? metaFormatter(dataValue) : dataValue;
            };
        }
        renderStatistic(chart, { statistic: statistic, plotType: 'pie' });
    }
    return params;
}
/**
 * 饼图适配器
 * @param chart
 * @param options
 */
export function adaptor(params) {
    // flow 的方式处理所有的配置到 G2 API
    return flow(geometry, meta, theme, coordinate, legend, tooltip, label, state, annotation(), 
    /** 指标卡中心文本 放在下层 */
    statistic, interaction, animation)(params);
}
//# sourceMappingURL=adaptor.js.map