'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var echarts = _interopDefault(require('echarts/lib/echarts'));
require('echarts/lib/component/tooltip');
require('echarts/lib/component/legend');
require('echarts/lib/component/title');
require('echarts/lib/chart/bar');
require('echarts/lib/chart/line');
require('echarts/lib/chart/pie');

var SIGN = '@_@';
var getLabelName = function getLabelName(item) {
  return item.split(SIGN)[0];
}; // TODO: delete
var getLegendName = function getLegendName(item) {
  return item.split(SIGN)[0];
};
var tipPointStyle = [// TODO: delete
'display: inline-block;', 'width: 10px;', 'height: 10px;', 'border-radius: 50%;', 'margin-right:2px;'].join('');

var itemPoint = function itemPoint(color) {
  return ['<span style="', 'background-color:' + color + ';', 'display: inline-block;', 'width: 10px;', 'height: 10px;', 'border-radius: 50%;', 'margin-right:2px;', '"></span>'].join('');
};

echarts.registerTheme('ve-chart', {
  color: ['#19d4ae', '#5ab1ef', '#fa6e86', '#ffb980', '#0067a6', '#c4b4e4', '#d87a80', '#9cbbff', '#d9d0c7', '#87a997', '#d49ea2', '#5b4947', '#7ba3a8'],
  categoryAxis: {
    axisLine: { show: false },
    axisTick: { show: false },
    splitLine: { show: false }
  },
  valueAxis: {
    axisLine: { show: false }
  },
  line: {
    smooth: true
  },
  grid: {
    left: 60,
    right: 60,
    top: 60,
    bottom: 60
  }
});

var numberFormat = function numberFormat(val) {
  var digits = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 2;

  if (isNaN(+val)) return val;

  var symbolMap = [{ value: 1E18, symbol: 'E' }, { value: 1E15, symbol: 'P' }, { value: 1E12, symbol: 'T' }, { value: 1E9, symbol: 'B' }, { value: 1E6, symbol: 'M' }, { value: 1E3, symbol: 'k' }];

  for (var i = 0; i < symbolMap.length; i++) {
    if (Math.abs(val) >= symbolMap[i].value) {
      return (val / symbolMap[i].value).toFixed(digits) + symbolMap[i].symbol;
    }
  }

  return val.toString();
};

var formatTausends = function formatTausends(num) {
  return String(num).replace(/^(\s+|-)?\d+(?=.?\d*($|\s))/g, function (m) {
    return m.replace(/(?=(?!\b)(\d{3})+$)/g, ',');
  });
};

var getFormated = function getFormated(val, type) {
  switch (type) {
    case 'KMB':
      return numberFormat(val);
    case 'percent':
      return (val * 100).toFixed(2) + '%';
    case 'normal':
      return formatTausends(val);
    default:
      return val;
  }
};

var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var dataHandler = {
  getBarLegends: function getBarLegends(_ref) {
    var measures = _ref.measures,
        axisSite = _ref.axisSite,
        meaAxisType = _ref.meaAxisType,
        isColumn = _ref.isColumn;

    var legends = [];

    var formatter = getLabelName;
    var secondAxis = isColumn ? axisSite.right : axisSite.top;
    measures.forEach(function (measure) {
      var legendItem = ~secondAxis.indexOf(measure) ? '' + measure + SIGN + meaAxisType[1] : '' + measure + SIGN + meaAxisType[0];
      legends.push(legendItem);
    });

    return legends.length ? { data: legends, formatter: formatter } : false;
  },
  getBarDimAxis: function getBarDimAxis(_ref2) {
    var rows = _ref2.rows,
        dimAxisName = _ref2.dimAxisName,
        dimensions = _ref2.dimensions;

    return dimensions.map(function (dimension) {
      return {
        type: 'category',
        name: dimAxisName || dimension,
        nameLocation: 'middle',
        nameGap: 22,
        data: rows.map(function (row) {
          return row[dimension];
        }),
        axisLabel: {
          formatter: function formatter(v) {
            return String(v);
          }
        }
      };
    });
  },
  getBarMeaAxis: function getBarMeaAxis(_ref3) {
    var columns = _ref3.columns,
        meaAxisName = _ref3.meaAxisName,
        measures = _ref3.measures,
        meaAxisType = _ref3.meaAxisType;

    var meaAxisBase = { type: 'value', axisTick: { show: false } };
    var meaAxis = [];

    var _loop = function _loop(i) {
      if (meaAxisType[i]) {
        meaAxis[i] = Object.assign({}, meaAxisBase, {
          axisLabel: {
            formatter: function formatter(val) {
              return getFormated(val, meaAxisType[i]);
            }
          }
        });
      } else {
        meaAxis[i] = Object.assign({}, meaAxisBase);
      }
      meaAxis[i].name = meaAxisName[i] || '';
    };

    for (var i = 0; i < 2; i++) {
      _loop(i);
    }

    return meaAxis;
  },
  getBarTooltip: function getBarTooltip() {
    return {
      trigger: 'axis',
      formatter: function formatter(items) {
        var tpl = [];
        var title = String(items[0].name).split(SIGN)[0];
        tpl.push(title + '<br>');
        items.forEach(function (item) {
          var _item$seriesName$spli = item.seriesName.split(SIGN),
              _item$seriesName$spli2 = _slicedToArray(_item$seriesName$spli, 2),
              name = _item$seriesName$spli2[0],
              type = _item$seriesName$spli2[1];

          tpl.push(itemPoint(item.color));
          tpl.push(name + ': ');
          tpl.push(getFormated(item.value, type));
          tpl.push('<br>');
        });

        return tpl.join('');
      }
    };
  },
  getStackMap: function getStackMap(stack) {
    var stackMap = {};
    Object.keys(stack).forEach(function (item) {
      stack[item].forEach(function (name) {
        stackMap[name] = item;
      });
    });
    return stackMap;
  },
  getBarSeries: function getBarSeries(_ref4) {
    var rows = _ref4.rows,
        measures = _ref4.measures,
        stackMap = _ref4.stackMap,
        axisSite = _ref4.axisSite,
        meaAxisType = _ref4.meaAxisType,
        isColumn = _ref4.isColumn;

    var series = [];
    var seriesTemp = {};
    var secondAxis = isColumn ? axisSite.right : axisSite.top;
    var secondDimAxisIndex = isColumn ? 'yAxisIndex' : 'xAxisIndex';
    measures.forEach(function (measure) {
      seriesTemp[measure] = [];
    });
    rows.forEach(function (row) {
      measures.forEach(function (measure) {
        seriesTemp[measure].push(row[measure]);
      });
    });
    series = Object.keys(seriesTemp).map(function (item) {
      var itemName = ~secondAxis.indexOf(item) ? '' + item + SIGN + meaAxisType[1] : '' + item + SIGN + meaAxisType[0];
      var seriesItem = _defineProperty({
        name: itemName,
        type: 'bar',
        data: seriesTemp[item]
      }, secondDimAxisIndex, ~secondAxis.indexOf(item) ? '1' : '0');

      if (stackMap[item]) seriesItem.stack = stackMap[item];

      return seriesItem;
    });

    return series.length ? series : false;
  }
};
var bar = function bar(data, settings) {
  if (!data || !Array.isArray(data.columns) || !Array.isArray(data.rows)) return false;
  var columns = data.columns,
      rows = data.rows;
  var _settings$axisSite = settings.axisSite,
      axisSite = _settings$axisSite === undefined ? { top: [] } : _settings$axisSite,
      _settings$dimensions = settings.dimensions,
      dimensions = _settings$dimensions === undefined ? [columns[0]] : _settings$dimensions,
      _settings$stack = settings.stack,
      stack = _settings$stack === undefined ? {} : _settings$stack;

  var measures = columns.slice();
  if (settings.measures) {
    measures = settings.measures;
  } else {
    measures.splice(columns.indexOf(dimensions[0]), 1);
  }
  var meaAxisType = settings.xAxisType || ['normal', 'normal'];
  var meaAxisName = settings.xAxisName || [];
  var dimAxisName = settings.yAxisName || dimensions[0];
  var isColumn = false;

  var stackMap = stack ? dataHandler.getStackMap(stack) : {};
  var legend = dataHandler.getBarLegends({ measures: measures, axisSite: axisSite, meaAxisType: meaAxisType, isColumn: isColumn });
  var yAxis = dataHandler.getBarDimAxis({ rows: rows, dimAxisName: dimAxisName, dimensions: dimensions });
  var xAxis = dataHandler.getBarMeaAxis({ columns: columns, meaAxisName: meaAxisName, measures: measures, meaAxisType: meaAxisType });
  var series = dataHandler.getBarSeries({ rows: rows, measures: measures, stackMap: stackMap, axisSite: axisSite, meaAxisType: meaAxisType, isColumn: isColumn });
  var tooltip = dataHandler.getBarTooltip();
  var options = { legend: legend, yAxis: yAxis, series: series, xAxis: xAxis, tooltip: tooltip };
  return options;
};

var column = function column(data, settings) {
  if (!data || !Array.isArray(data.columns) || !Array.isArray(data.rows)) return false;
  var columns = data.columns,
      rows = data.rows;
  var _settings$axisSite2 = settings.axisSite,
      axisSite = _settings$axisSite2 === undefined ? { right: [] } : _settings$axisSite2,
      _settings$dimensions2 = settings.dimensions,
      dimensions = _settings$dimensions2 === undefined ? [columns[0]] : _settings$dimensions2,
      _settings$stack2 = settings.stack,
      stack = _settings$stack2 === undefined ? {} : _settings$stack2;

  var measures = columns.slice();
  if (settings.measures) {
    measures = settings.measures;
  } else {
    measures.splice(columns.indexOf(dimensions[0]), 1);
  }
  var meaAxisType = settings.yAxisType || ['normal', 'normal'];
  var meaAxisName = settings.yAxisName || [];
  var dimAxisName = settings.xAxisName || dimensions[0];
  var isColumn = true;

  var stackMap = stack ? dataHandler.getStackMap(stack) : {};
  var legend = dataHandler.getBarLegends({ measures: measures, axisSite: axisSite, meaAxisType: meaAxisType, isColumn: isColumn });
  var xAxis = dataHandler.getBarDimAxis({ rows: rows, dimAxisName: dimAxisName, dimensions: dimensions });
  var yAxis = dataHandler.getBarMeaAxis({ columns: columns, meaAxisName: meaAxisName, measures: measures, meaAxisType: meaAxisType });
  var series = dataHandler.getBarSeries({ rows: rows, measures: measures, stackMap: stackMap, axisSite: axisSite, meaAxisType: meaAxisType, isColumn: isColumn });
  var tooltip = dataHandler.getBarTooltip();
  var options = { legend: legend, yAxis: yAxis, series: series, xAxis: xAxis, tooltip: tooltip };
  return options;
};

var _slicedToArray$1 = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();

var dataHandler$1 = {
  getLineLegends: function getLineLegends(_ref) {
    var measures = _ref.measures,
        axisSite = _ref.axisSite,
        yAxisType = _ref.yAxisType;

    var legends = [];

    var formatter = getLegendName;

    measures.forEach(function (measure) {
      var legendItem = ~axisSite.right.indexOf(measure) ? '' + measure + SIGN + yAxisType[1] : '' + measure + SIGN + yAxisType[0];
      legends.push(legendItem);
    });

    return legends.length ? { data: legends, formatter: formatter } : false;
  },
  getLineXAxis: function getLineXAxis(_ref2) {
    var dimensions = _ref2.dimensions,
        rows = _ref2.rows,
        xAxisName = _ref2.xAxisName;

    return dimensions.map(function (dimension, index) {
      return {
        type: 'category',
        nameLocation: 'middle',
        nameGap: 22,
        name: xAxisName[index] || dimension,
        axisTick: { show: true, lineStyle: { color: '#eee' } },
        data: rows.map(function (row) {
          return row[dimension];
        }),
        axisLabel: {
          formatter: function formatter(v) {
            return String(v);
          }
        }
      };
    });
  },
  getLineSeries: function getLineSeries(_ref3) {
    var rows = _ref3.rows,
        axisSite = _ref3.axisSite,
        yAxisType = _ref3.yAxisType,
        dimensions = _ref3.dimensions,
        measures = _ref3.measures;

    var series = [];
    var dataTemp = {};
    measures.forEach(function (measure) {
      dataTemp[measure] = [];
    });
    rows.forEach(function (row) {
      measures.forEach(function (measure) {
        dataTemp[measure].push(row[measure] || 0);
      });
    });
    measures.forEach(function (measure) {
      var seriesItem = {
        name: measure,
        type: 'line',
        data: dataTemp[measure]
      };

      if (~axisSite.right.indexOf(measure)) {
        seriesItem.yAxisIndex = 1;
        seriesItem.name = '' + measure + SIGN + yAxisType[1];
      } else {
        seriesItem.yAxisIndex = 0;
        seriesItem.name = '' + measure + SIGN + yAxisType[0];
      }

      series.push(seriesItem);
    });
    return series.length ? series : false;
  },
  getLineYAxis: function getLineYAxis(_ref4) {
    var yAxisName = _ref4.yAxisName,
        yAxisType = _ref4.yAxisType;

    var yAxisBase = { type: 'value', axisTick: { show: false } };
    var yAxis = [];

    var _loop = function _loop(i) {
      if (yAxisType[i]) {
        yAxis[i] = Object.assign({}, yAxisBase, {
          axisLabel: {
            formatter: function formatter(val) {
              return getFormated(val, yAxisType[i]);
            }
          }
        });
      } else {
        yAxis[i] = Object.assign({}, yAxisBase);
      }
      yAxis[i].name = yAxisName[i] || '';
    };

    for (var i = 0; i < 2; i++) {
      _loop(i);
    }
    return yAxis;
  },
  getLineTooltip: function getLineTooltip() {
    return {
      trigger: 'axis',
      formatter: function formatter(items) {
        var tpl = [];
        tpl.push(items[0].name + '<br>');
        items.forEach(function (item) {
          var showData = void 0;

          var _item$seriesName$spli = item.seriesName.split(SIGN),
              _item$seriesName$spli2 = _slicedToArray$1(_item$seriesName$spli, 2),
              name = _item$seriesName$spli2[0],
              type = _item$seriesName$spli2[1];

          showData = getFormated(item.data, type);
          tpl.push(itemPoint(item.color));
          tpl.push(name + ': ' + showData);
          tpl.push('<br>');
        });
        return tpl.join('');
      }
    };
  }
};

var line = function line(data, settings) {
  if (!data || !Array.isArray(data.columns) || !Array.isArray(data.rows)) return false;
  var columns = data.columns,
      rows = data.rows;
  var _settings$axisSite = settings.axisSite,
      axisSite = _settings$axisSite === undefined ? { right: [] } : _settings$axisSite,
      _settings$yAxisType = settings.yAxisType,
      yAxisType = _settings$yAxisType === undefined ? ['normal', 'normal'] : _settings$yAxisType,
      _settings$yAxisName = settings.yAxisName,
      yAxisName = _settings$yAxisName === undefined ? [] : _settings$yAxisName,
      _settings$dimensions = settings.dimensions,
      dimensions = _settings$dimensions === undefined ? [columns[0]] : _settings$dimensions,
      _settings$xAxisName = settings.xAxisName,
      xAxisName = _settings$xAxisName === undefined ? dimensions : _settings$xAxisName;

  var measures = columns.slice();
  if (settings.measures) {
    measures = settings.measures;
  } else {
    measures.splice(columns.indexOf(dimensions[0]), 1);
  }
  var legend = dataHandler$1.getLineLegends({ measures: measures, axisSite: axisSite, yAxisType: yAxisType });
  var tooltip = dataHandler$1.getLineTooltip();
  var xAxis = dataHandler$1.getLineXAxis({ dimensions: dimensions, rows: rows, xAxisName: xAxisName });
  var yAxis = dataHandler$1.getLineYAxis({ yAxisName: yAxisName, yAxisType: yAxisType });
  var series = dataHandler$1.getLineSeries({ rows: rows, axisSite: axisSite, yAxisType: yAxisType, dimensions: dimensions, measures: measures });
  if (!legend || !xAxis || !series) return false;

  var options = { legend: legend, xAxis: xAxis, series: series, yAxis: yAxis, tooltip: tooltip };
  return options;
};

var pieRadius = 100;
var ringRadius = [80, 100];
var pieOffsetY = 200;

var dataHandler$2 = {
  getPieSeries: function getPieSeries(args) {
    var rows = args.rows,
        dataType = args.dataType,
        percentShow = args.percentShow,
        dimension = args.dimension,
        measure = args.measure,
        radius = args.radius,
        offsetY = args.offsetY;


    var series = {
      type: 'pie',
      radius: radius,
      data: [],
      center: ['50%', offsetY]
    };
    if (percentShow) {
      series.label = {
        normal: {
          show: true,
          formatter: function formatter(item) {
            var tpl = [];
            tpl.push(item.name + ':');
            tpl.push(getFormated(item.value, dataType));
            tpl.push('(' + item.percent + '%)');
            return tpl.join(' ');
          }
        }
      };
    }
    series.data = rows.map(function (row) {
      return { name: row[dimension], value: row[measure] };
    });

    return series;
  },
  getPieLegend: function getPieLegend(_ref) {
    var rows = _ref.rows,
        dimension = _ref.dimension,
        legendLimit = _ref.legendLimit;

    var legend = rows.map(function (row) {
      return row[dimension];
    });
    return legend.length ? { data: legend, show: legend.length < legendLimit } : false;
  },
  getPieTooltip: function getPieTooltip(dataType) {
    return {
      formatter: function formatter(item) {
        var tpl = [];
        tpl.push(itemPoint(item.color));
        tpl.push(item.name + ':');
        tpl.push(getFormated(item.value, dataType));
        tpl.push('(' + item.percent + '%)');
        return tpl.join(' ');
      }
    };
  }
};

var pie = function pie(data, settings, isRing) {
  if (!data || !Array.isArray(data.columns) || !Array.isArray(data.rows)) return false;

  var columns = data.columns,
      rows = data.rows;
  var _settings$dataType = settings.dataType,
      dataType = _settings$dataType === undefined ? 'normal' : _settings$dataType,
      percentShow = settings.percentShow,
      _settings$dimension = settings.dimension,
      dimension = _settings$dimension === undefined ? columns[0] : _settings$dimension,
      _settings$measure = settings.measure,
      measure = _settings$measure === undefined ? columns[1] : _settings$measure,
      _settings$radius = settings.radius,
      radius = _settings$radius === undefined ? isRing ? ringRadius : pieRadius : _settings$radius,
      _settings$offsetY = settings.offsetY,
      offsetY = _settings$offsetY === undefined ? pieOffsetY : _settings$offsetY,
      _settings$legendLimit = settings.legendLimit,
      legendLimit = _settings$legendLimit === undefined ? 30 : _settings$legendLimit;


  var series = dataHandler$2.getPieSeries({
    rows: rows, dataType: dataType, percentShow: percentShow, dimension: dimension, measure: measure, radius: radius, offsetY: offsetY
  });
  var legend = dataHandler$2.getPieLegend({ rows: rows, dimension: dimension, legendLimit: legendLimit });
  var tooltip = dataHandler$2.getPieTooltip(dataType);
  if (!legend) return false;
  var options = { series: series, legend: legend, tooltip: tooltip };
  return options;
};

var ring = function ring(data, settings) {
  return pie(data, settings, true);
};

var chartMixin = {
  props: {
    data: { type: [Object, Array], default: null },
    settings: { type: Object, default: function _default() {
        return {};
      }
    },
    width: { type: String, default: 'auto' },
    height: { type: String, default: '400px' },
    beforeConfig: { type: Function },
    afterConfig: { type: Function },
    events: { type: Object }
  },

  watch: {
    data: function data(v) {
      var dataKeys = Object.keys(v);
      var dataKeyProp = v.key;
      if (dataKeyProp && Array.isArray(dataKeyProp) && dataKeyProp.length || dataKeys.length) {
        this.dataHandler(v);
      }
    },

    settings: {
      deep: true,
      handler: function handler(v) {
        this.chartHandler = this.chartLib[v.type];
        this.dataHandler(this.data);
      }
    }
  },

  computed: {
    canvasStyle: function canvasStyle() {
      return { width: this.width, height: this.height };
    }
  },

  methods: {
    dataHandler: function dataHandler(data) {
      if (!this.chartHandler) return;
      if (this.beforeConfig) data = this.beforeConfig(data);
      var options = this.chartHandler(data, this.settings);
      if (Array.isArray(this.settings.color)) options.color = this.settings.color;
      if (this.settings.grid) options.grid = this.settings.grid;
      if (this.afterConfig) options = this.afterConfig(options);
      if (options) this.echarts.setOption(options, true);
    },
    init: function init() {
      if (this.echarts) return;
      this.echarts = this.echartsLib.init(this.$refs.canvas, 've-chart');
      if (this.data) this.dataHandler(this.data);
      if (this.events) this.bindEvents();
    },
    bindEvents: function bindEvents() {
      var _this = this;

      Object.keys(this.events).forEach(function (event) {
        _this.echarts.on(event, _this.events[event]);
      });
    }
  },

  mounted: function mounted() {
    var _this2 = this;

    this.$nextTick(function () {
      _this2.init();
      window.addEventListener('resize', _this2.echarts.resize);
    });
  },
  beforeDestory: function beforeDestory() {
    window.removeEventListener('resize', this.echarts.resize);
    this.echarts.dispose();
  }
};

var index = {
  render: function render() {
    var _vm = this;var _h = _vm.$createElement;var _c = _vm._self._c || _h;return _c('div', { ref: "canvas", style: _vm.canvasStyle });
  },
  staticRenderFns: [],
  name: 'VeChart',
  mixins: [chartMixin],
  created: function created() {
    this.chartLib = { bar: bar, column: column, line: line, pie: pie, ring: ring };
    this.chartHandler = this.chartLib[this.settings.type];
    this.echartsLib = echarts;
  }
};

module.exports = index;
