/**
 * 
 *     @dfsj/echarts： 专业的水文曲线组件或构造函数以及通用的echart二次封装图表
 *     版本: v3.7.0
 *     作者：yangbo <1747837358@qq.com>
 *     日期：2025-05-27 15:53:33
 *     
 *     
 */

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

function useHydrology(chartRef, override) {
  var _override$C;
  var _ref = chartRef ? useECharts(chartRef) : {},
    _ref$setOptions = _ref.setOptions,
    setOptions = _ref$setOptions === void 0 ? null : _ref$setOptions,
    _ref$echarts = _ref.echarts,
    echarts = _ref$echarts === void 0 ? null : _ref$echarts,
    _ref$getInstance = _ref.getInstance,
    getInstance = _ref$getInstance === void 0 ? function () {} : _ref$getInstance,
    _ref$container = _ref.container,
    container = _ref$container === void 0 ? null : _ref$container;
  var _useTable = useTable(),
    props = _useTable.props,
    toggleTable = _useTable.toggleTable;
  props.container = container;
  var _useStatistic = useStatistic(),
    statisticProps = _useStatistic.props,
    toggleStatistic = _useStatistic.toggleStatistic;
  var assemble = function assemble(valueOri, options) {
    var _valueOri;
    var chart = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : getInstance();
    valueOri = (_valueOri = valueOri) !== null && _valueOri !== void 0 ? _valueOri : {};
    var value = JSON.parse(JSON.stringify(valueOri));
    var optioned = assign({}, config$1, options);
    if (value != null) {
      var _value$filterCodes, _value$dimensions2, _optioned$series, _optioned$series2, _optioned$series3;
      props.value = value;
      props.chart = chart;
      statisticProps.chart = chart;
      statisticProps.value = value;
      if ((value === null || value === void 0 || (_value$filterCodes = value.filterCodes) === null || _value$filterCodes === void 0 ? void 0 : _value$filterCodes.length) > 0) {
        var _value$dimensions;
        value.dimensions = value === null || value === void 0 || (_value$dimensions = value.dimensions) === null || _value$dimensions === void 0 ? void 0 : _value$dimensions.filter(function (e) {
          return !value.filterCodes.includes((e === null || e === void 0 ? void 0 : e.code) || e);
        });
      }
      optioned.dataset.source = value.source;
      optioned.series = (_value$dimensions2 = value.dimensions) === null || _value$dimensions2 === void 0 ? void 0 : _value$dimensions2.filter(function (e, i) {
        return i;
      }).map(function (e, i) {
        var encode = {
            x: 0,
            y: i + 1
          },
          yAxisIndex = optioned.yAxis.findIndex(function (v) {
            return v && v.$layout & e.code;
          });
        return assign({
          yAxisIndex: yAxisIndex,
          encode: encode,
          name: e.name,
          selected: e.selected
        }, toSeries(e, i, optioned));
      });
      var indices = toIndices$1(value, optioned);
      if (indices !== null && indices !== void 0 && indices.length) (_optioned$series = optioned.series).push.apply(_optioned$series, _toConsumableArray(indices));
      var marks = toMarks$1(value, optioned);
      if (marks !== null && marks !== void 0 && marks.length) {
        optioned.series.push(assign({
          data: marks
        }, config.mark.series));
      }
      optioned.legend.data = optioned === null || optioned === void 0 || (_optioned$series2 = optioned.series) === null || _optioned$series2 === void 0 ? void 0 : _optioned$series2.map(function (e) {
        return {
          name: e.name,
          textStyle: {
            color: e.color
          }
        };
      });
      optioned.legend.selected = (_optioned$series3 = optioned.series) === null || _optioned$series3 === void 0 ? void 0 : _optioned$series3.reduce(function (p, v) {
        return p[v.name] = v.selected != null ? v.selected : true, p;
      }, {});
    }
    return optioned;
  };
  var toSeries = override !== null && override !== void 0 && override.S ? override.S : function (dimension, index, optioned) {
    return optioned.$series.find(function (e) {
      return e.$code & dimension.code;
    });
  };
  var toMarks$1 = override !== null && override !== void 0 && override.M ? override.M : function (value, optioned) {
    return toMarks(value, optioned, config.mark);
  };
  var toIndices$1 = override !== null && override !== void 0 && override.I ? override.I : function (value, optioned) {
    return toIndices(value, optioned);
  };
  var config$1 = (_override$C = override === null || override === void 0 ? void 0 : override.C) !== null && _override$C !== void 0 ? _override$C : def;
  var createOptions = function createOptions(data) {
    if (data == null) return null;
    var flo = complexMinMaxIndex(data, CATEGORY.FLOW);
    var stg = complexMinMaxIndex(data, CATEGORY.STAGE);
    return {
      yAxis: [{
        layout: CATEGORY.FLOW,
        name: '流量(m³/s)',
        min: function min(v) {
          return minimum(v, flo);
        },
        max: function max(v) {
          return maximum(v, flo);
        }
      }, {
        layout: CATEGORY.STAGE,
        name: '水位(m)',
        min: function min(v) {
          return minimum(v, stg);
        },
        max: function max(v) {
          return maximum(v, stg);
        }
      }]
    };
  };
  return {
    createOptions: createOptions,
    setOptions: setOptions,
    echarts: echarts,
    getInstance: getInstance,
    container: container,
    assemble: assemble,
    toSeries: toSeries,
    toMarks: toMarks$1,
    toIndices: toIndices$1,
    toggleTable: toggleTable,
    toggleStatistic: toggleStatistic
  };
}

export { useHydrology };
