import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
import assign from '../../utils/assign.js';
import config, { CATEGORY } from '../../config.js';
import { toIndices } from '../../utils/render.js';
import { ensure, complexMinMaxIndex, minimum, maximum } from '../../utils/tools.js';
import def from './default.js';
import { constant } from '@dfsj/utils';
import { useECharts } from '../../hooks/useECharts.js';
import { useTable } from '../../view/useTable.js';
import { useStatistic } from '../../view/useStatistic.js';

function useComplexHydrology(chartRef) {
  var _useECharts = useECharts(chartRef),
    setOptions = _useECharts.setOptions,
    echarts = _useECharts.echarts,
    getInstance = _useECharts.getInstance,
    container = _useECharts.container;
  var _useTable = useTable(),
    props = _useTable.props,
    toggleTable = _useTable.toggleTable;
  props.container = container;
  var _useStatistic = useStatistic(),
    statisticProps = _useStatistic.props,
    toggleStatistic = _useStatistic.toggleStatistic;
  console.log('---C', constant);
  var assemble = function assemble(value, options) {
    var chart = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : getInstance();
    var optioned = assign({}, def, options);
    if (value != null) {
      var _optioned$series;
      props.value = value;
      props.chart = chart;
      statisticProps.chart = chart;
      statisticProps.value = value;
      var counter = 0;
      var axis = optioned.yAxis;
      var yAxisCodes = [axis[0].$layout | axis[1].$layout, axis[2].$layout | axis[3].$layout];
      optioned.dataset.source = value.source;
      optioned.series = value.dimensions.filter(function (e, i) {
        return i > 0;
      }).map(function (e, i) {
        var yAxisIndex = optioned.yAxis.findIndex(function (v) {
          return v.$layout & e.code;
        });
        var xAxisIndex = yAxisCodes.findIndex(function (v) {
          return v & e.code;
        });
        var series = optioned.$series.find(function (s) {
          return s.$code & e.code;
        });
        var encode = {
          x: 0,
          y: i + 1
        };
        var color = !!(e.code & CATEGORY.RAINFALL) ? config.rainfallColor[counter++] : undefined;
        return assign({
          yAxisIndex: yAxisIndex,
          xAxisIndex: xAxisIndex,
          color: color,
          encode: encode,
          name: e.name,
          selected: e.selected,
          visible: true
        }, series);
      });
      var indices = toIndices(value, optioned, yAxisCodes);
      if (indices !== null && indices !== void 0 && indices.length) (_optioned$series = optioned.series).push.apply(_optioned$series, _toConsumableArray(indices));
      var marks = toMarks(value, optioned);
      if (marks !== null && marks !== void 0 && marks.length) {
        optioned.series.push(assign({
          data: marks
        }, config.mark.series));
      }
      optioned.legend.data = optioned.series.map(function (e) {
        return {
          name: e.name,
          textStyle: {
            color: e.color
          }
        };
      });
      optioned.legend.selected = optioned.series.reduce(function (p, v) {
        return p[v.name] = v.selected != null ? v.selected : true, p;
      }, {});
    }
    return optioned;
  };
  var toMarks = function toMarks(value, optioned) {
    var _value$areas;
    var areas = ((_value$areas = value.areas) === null || _value$areas === void 0 ? void 0 : _value$areas.map(function (e) {
      return assign({
        shape: 'rect',
        value: e.time,
        gridIndex: [0, 1],
        itemStyle: {
          fill: e.back || config.areaColor[e.type]
        }
      }, config.mark.shape.rect);
    })) || constant.EMPTY_ARRAY;
    var splits = ensure(value.split).reduce(function (accept, split) {
      var _optioned$splitFormat, _optioned$splitFormat2, _optioned$markOffset;
      return accept.concat([assign({
        shape: 'line',
        value: split,
        gridIndex: [0],
        xAxisIndex: 0,
        yAxisIndex: 0
      }, config.mark.shape.line), assign({
        shape: 'line',
        value: split,
        gridIndex: [1],
        xAxisIndex: 1,
        yAxisIndex: 2
      }, config.mark.shape.line), assign({
        shape: 'text',
        value: split,
        gridIndex: [1],
        xAxisIndex: 1,
        yAxisIndex: 2,
        label: {
          text: (_optioned$splitFormat = optioned === null || optioned === void 0 || (_optioned$splitFormat2 = optioned.splitFormat) === null || _optioned$splitFormat2 === void 0 ? void 0 : _optioned$splitFormat2.call(optioned, split)) !== null && _optioned$splitFormat !== void 0 ? _optioned$splitFormat : split,
          offset: (_optioned$markOffset = optioned === null || optioned === void 0 ? void 0 : optioned.markOffset) !== null && _optioned$markOffset !== void 0 ? _optioned$markOffset : [0, '-7%']
        }
      }, config.mark.shape.text)]);
    }, []);
    return areas.concat(splits);
  };
  var createOptions = function createOptions(data) {
    if (data == null) return null;
    var flo = complexMinMaxIndex(data, CATEGORY.FLOW);
    return {
      yAxis: [{
        layout: CATEGORY.RAINFALL,
        name: '降雨量(mm)',
        min: 0,
        max: function max(v) {
          return Math.round(v.max + (v.max - v.min) * 0.1);
        }
      }, {
        layout: CATEGORY.SWC,
        name: '土壤含水量(%)',
        min: 0,
        max: function max(v) {
          return Math.round(v.max + (v.max - v.min) * 0.1);
        }
      }, {
        layout: CATEGORY.FLOW,
        name: '流量(m³/s)',
        min: function min(v) {
          return minimum(v, flo);
        },
        max: function max(v) {
          return maximum(v, flo);
        }
      }, {
        layout: '',
        name: ''
      }]
    };
  };
  return {
    setOptions: setOptions,
    echarts: echarts,
    getInstance: getInstance,
    container: container,
    createOptions: createOptions,
    assemble: assemble,
    toggleTable: toggleTable,
    toggleStatistic: toggleStatistic
  };
}

export { useComplexHydrology };
