"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.adaptor = void 0;
var tslib_1 = require("tslib");
var util_1 = require("@antv/util");
var common_1 = require("../../adaptor/common");
var geometries_1 = require("../../adaptor/geometries");
var utils_1 = require("../../utils");
var utils_2 = require("./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 = util_1.filter(data, function (d) { return typeof d[angleField] === 'number' || util_1.isNil(d[angleField]); });
    // 打印异常数据情况
    utils_1.log(utils_1.LEVEL.WARN, processData.length === data.length, 'illegal data existed in chart data.');
    var allZero = util_1.every(processData, function (d) { return d[angleField] === 0; });
    if (allZero) {
        // 数据全 0 处理，调整 position 映射
        var percentageField_1 = '$$percentage$$';
        processData = processData.map(function (d) {
            var _a;
            return (tslib_1.__assign(tslib_1.__assign({}, d), (_a = {}, _a[percentageField_1] = 1 / processData.length, _a)));
        });
        chart.data(processData);
        var p = utils_1.deepAssign({}, params, {
            options: {
                xField: '1',
                yField: percentageField_1,
                seriesField: colorField,
                isStack: true,
                interval: {
                    color: color,
                    style: pieStyle,
                },
            },
        });
        geometries_1.interval(p);
        // all zero 额外处理
        chart.geometries[0].tooltip(colorField + "*" + angleField);
    }
    else {
        chart.data(processData);
        var p = utils_1.deepAssign({}, params, {
            options: {
                xField: '1',
                yField: angleField,
                seriesField: colorField,
                isStack: true,
                interval: {
                    color: color,
                    style: pieStyle,
                },
            },
        });
        geometries_1.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 = utils_1.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 = tslib_1.__rest(label, ["callback"]);
        var labelCfg = utils_1.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 util_1.isFunction(content_1)
                    ? // append pecent (number) to data, users can get origin data from `dataum._origin`
                        content_1(tslib_1.__assign(tslib_1.__assign({}, data), { percent: percent }), dataum, index)
                    : util_1.isString(content_1)
                        ? utils_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 = utils_1.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: tslib_1.__assign(tslib_1.__assign({}, labelCfg), { offset: utils_2.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 && !util_1.get(title, 'formatter')) {
            // @ts-ignore
            title.formatter = function (datum) { return (datum ? datum[colorField] : '总计'); };
        }
        if (content !== false && !util_1.get(content, 'formatter')) {
            // @ts-ignore
            content.formatter = function (datum, data) {
                var metaFormatter = util_1.get(meta, [angleField, 'formatter']);
                var dataValue = datum ? datum[angleField] : utils_2.getTotalValue(data, angleField);
                return metaFormatter ? metaFormatter(dataValue) : dataValue;
            };
        }
        utils_1.renderStatistic(chart, { statistic: statistic, plotType: 'pie' });
    }
    return params;
}
/**
 * 饼图适配器
 * @param chart
 * @param options
 */
function adaptor(params) {
    // flow 的方式处理所有的配置到 G2 API
    return utils_1.flow(geometry, meta, common_1.theme, coordinate, common_1.legend, common_1.tooltip, label, common_1.state, common_1.annotation(), 
    /** 指标卡中心文本 放在下层 */
    statistic, common_1.interaction, common_1.animation)(params);
}
exports.adaptor = adaptor;
//# sourceMappingURL=adaptor.js.map