import { complexMinMaxIndex, minimum, maximum, getNearNDays, numeralFormat } from '../utils/tools.js';
import { CATEGORY } from '../config.js';

var units = [{
  value: 1e4,
  symbol: '亿m³'
}, {
  value: 1e2,
  symbol: '百万m³'
}, {
  value: 0,
  symbol: '万m³'
}];
function useAssist() {
  var config = {
    15: {
      forec: {
        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: ''
            }]
          };
        },
        transform: function transform(data) {
          return data;
        }
      },
      real: {
        createOptions: function createOptions(data) {
          if (data == null) return null;
          var flo = complexMinMaxIndex(data, CATEGORY.FLOW);
          var stg = complexMinMaxIndex(data, CATEGORY.STAGE);
          var day = getNearNDays(data.source, 7);
          var _min = Math.min.apply(null, stg);
          var _max = Math.max.apply(null, stg);
          return {
            dataZoom: [{
              type: 'slider',
              startValue: day[0],
              endValue: day[1]
            }, {
              type: 'inside',
              startValue: day[0],
              endValue: day[1]
            }],
            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.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) {
                var _data$indices;
                if (Number.isNaN(v.max) && Number.isNaN(v.min) && (_data$indices = data.indices) !== null && _data$indices !== void 0 && _data$indices.length) {
                  return minimum({
                    max: _max,
                    min: _min
                  }, stg);
                }
                return minimum(v, stg);
              },
              max: function max(v) {
                var _data$indices2;
                if (Number.isNaN(v.max) && Number.isNaN(v.min) && (_data$indices2 = data.indices) !== null && _data$indices2 !== void 0 && _data$indices2.length) {
                  return maximum({
                    max: _max,
                    min: _min
                  }, stg);
                }
                return maximum(v, stg);
              }
            }]
          };
        },
        transform: function transform(data) {
          return data;
        }
      }
    },
    1: {
      forec: {
        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: '',
              name: ''
            }, {
              $layout: CATEGORY.FLOW,
              name: '流量(m³/s)',
              min: function min(v) {
                return minimum(v, flo);
              },
              max: function max(v) {
                return maximum(v, flo);
              }
            }, {
              $layout: 0,
              name: ''
            }]
          };
        },
        transform: function transform(data) {
          return data;
        }
      },
      real: {
        createOptions: function createOptions(data) {
          if (data == null) return null;
          var flo = complexMinMaxIndex(data, CATEGORY.FLOW);
          var stg = complexMinMaxIndex(data, CATEGORY.STAGE);
          var cap = complexMinMaxIndex(data, CATEGORY.CAPACITY);
          var day = getNearNDays(data.source || [], -7);
          return {
            dataZoom: [{
              type: 'slider',
              startValue: day[0],
              endValue: day[1]
            }, {
              type: 'inside',
              startValue: day[0],
              endValue: day[1]
            }],
            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.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);
              }
            }, {
              $layout: CATEGORY.CAPACITY,
              name: "\u5E93\u5BB9\uFF08".concat(data.unit.symbol, "\uFF09"),
              min: function min(v) {
                return minimum(v, cap);
              },
              max: function max(v) {
                return maximum(v, cap);
              }
            }]
          };
        },
        transform: function transform(data) {
          if (data == null) return null;
          data.unit = units[2];
          var idx = data.dimensions.findIndex(function (e) {
            return e.code === CATEGORY.CAPACITY;
          });
          if (idx > -1) {
            var _maximum = data.source.reduce(function (p, v) {
              return v[idx] > p ? v[idx] : p;
            }, 0);
            var unit = units.find(function (e) {
              return _maximum >= e.value;
            });
            if (unit.value > 0) {
              data.source.forEach(function (e) {
                return numeralFormat(e[idx] /= unit.value);
              });
              data.dimensions[idx].name = "\u5E93\u5BB9\uFF08".concat(unit.symbol, "\uFF09");
              data.unit = unit;
              if (data.indices) {
                var index = data.indices.find(function (e) {
                  return e.code === CATEGORY.CAPACITY;
                });
                if (index) {
                  index.indices.forEach(function (e) {
                    return e.value /= unit.value;
                  });
                  index.unit = "\uFF08".concat(unit.symbol, "\uFF09");
                }
              }
            }
          }
          return data;
        }
      },
      dispatch: {
        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);
              }
            }]
          };
        }
      }
    }
  };
  var getExistConfig = function getExistConfig(motype) {
    return config[motype] || {};
  };
  return {
    getExistConfig: getExistConfig
  };
}

export { useAssist };
