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

import dayjs from 'dayjs';
import numeral from 'numeral';
import assign from 'lodash-es/assign';
import { constant } from '@dfsj/utils';
import units from './units.js';

function numeralFormat(value) {
  var pattern = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '0.00';
  var def = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '--';
  return value == null ? def : numeral(value).format(pattern);
}
function to(value) {
  var pattern = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '0.00';
  var def = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '--';
  if (isNaN(value)) {
    return value || def;
  }
  return value != null ? numeralFormat(value, pattern) : def;
}
function ensure(value) {
  return value == null ? constant.EMPTY_ARRAY : Array.isArray(value) ? value : [value];
}
function avg(source, index) {
  var start = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var end = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : source === null || source === void 0 ? void 0 : source.length;
  var sum = 0,
    count = 0;
  for (var i = start; i < end; i++) {
    var value = source[i][index];
    if (value != null) sum += value, count++;
  }
  return count > 0 ? sum / count : null;
}
function sum(source, index) {
  var start = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var end = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : source.length;
  var num = 0;
  for (var i = start; i <= end; i++) {
    var value = source[i][index];
    if (value != null) num += value;
  }
  return num;
}
function min(source, index) {
  var start = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var end = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : source.length;
  var num = Number.POSITIVE_INFINITY;
  for (var i = start; i <= end; i++) {
    var _source$i;
    var value = source === null || source === void 0 || (_source$i = source[i]) === null || _source$i === void 0 ? void 0 : _source$i[index];
    if (value != null && value < num) {
      num = value;
    }
  }
  return num === Number.POSITIVE_INFINITY ? null : num;
}
function max(source, index) {
  var start = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var end = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : source.length;
  var num = Number.NEGATIVE_INFINITY;
  for (var i = start; i <= end; i++) {
    var _source$i2;
    var value = source === null || source === void 0 || (_source$i2 = source[i]) === null || _source$i2 === void 0 ? void 0 : _source$i2[index];
    if (value != null && value > num) {
      num = value;
    }
  }
  return num === Number.NEGATIVE_INFINITY ? null : num;
}
function toArea(map, area, source) {
  return area === null || area === void 0 ? void 0 : area.map(function (e) {
    return assign({
      index: [source.findIndex(function (x) {
        return x[0] === e.time[0];
      }), source.findIndex(function (x) {
        return x[0] === e.time[1];
      })]
    }, map[e.type]);
  });
}
function findNowIndex(source) {
  var now = dayjs(Date.now()).valueOf();
  var min = Number.MAX_VALUE,
    index = 0;
  for (var i = 0, ii = source.length; i < ii; i++) {
    var time = dayjs(source[i][0]);
    var diff = Math.abs(time.diff(now, 'milliseconds').valueOf());
    if (diff < min && time.valueOf() - now.valueOf() > 0) {
      min = diff;
      index = i;
    }
  }
  return index;
}
function findDataIndex(target, source) {
  var index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var start = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 0;
  var end = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : source.length;
  var extractedData = source.reduce(function (acc, row, i) {
    if (i >= start && i <= end) {
      acc.push(row[index]);
    }
    return acc;
  }, []);
  return extractedData.findIndex(function (e) {
    return e == +target;
  });
}
function complexMinMaxIndex(value, code) {
  if (value != null && value.indices != null) {
    var item = value.indices.find(function (e) {
      return e.code === code;
    });
    if (item != null) {
      return [item.indices.reduce(function (p, v) {
        return v.value > 0 && v.value < p ? v.value : p;
      }, Infinity), item.indices.reduce(function (p, v) {
        return v.value > 0 && v.value > p ? v.value : p;
      }, -Infinity)];
    }
  }
  return [Infinity, -Infinity];
}
function simpleMinMaxIndex(value) {
  if (value != null && value.indices != null) {
    return [value.indices.reduce(function (p, v) {
      return v.value > 0 && v.value < p ? v.value : p;
    }, Infinity), value.indices.reduce(function (p, v) {
      return v.value > 0 && v.value > p ? v.value : p;
    }, -Infinity)];
  }
  return [Infinity, -Infinity];
}
function minimum(v, a) {
  var limit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  return +numeral(Math.max(Math.min(v.min, a[0]) - (Math.max(v.max, a[1]) - Math.min(v.min, a[0])) * 0.1, limit)).format('0.00');
}
function maximum(v, a) {
  var limit = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : Infinity;
  return +numeral(Math.min(Math.max(v.max, a[1]) + (Math.max(v.max, a[1]) - Math.min(v.min, a[0])) * 0.1, limit)).format('0.00');
}
function getNearNDays(data) {
  var day = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  if ((data === null || data === void 0 ? void 0 : data.length) > 0) {
    var last = dayjs(data[data.length - 1]);
    var index = data.findIndex(function (row) {
      return last.diff(dayjs(row[0]), 'day') >= day;
    });
    return [index, data.length - 1];
  }
  return constant.EMPTY_ARRAY;
}
function adjustCapacity(capacity) {
  if (capacity != null) {
    var item = units.find(function (e) {
      return capacity >= e.value;
    });
    if (item) return numeral(capacity / item.value).format('0.000');
  }
  return capacity;
}
function adjustCapacityUnit(capacity) {
  if (capacity != null) {
    var item = units.find(function (e) {
      return capacity >= e.value;
    });
    if (item) return item.symbol;
  }
  return '万m³';
}

export { adjustCapacity, adjustCapacityUnit, avg, complexMinMaxIndex, ensure, findDataIndex, findNowIndex, getNearNDays, max, maximum, min, minimum, numeralFormat, simpleMinMaxIndex, sum, to, toArea };
