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

import _defineProperty from '@babel/runtime/helpers/defineProperty';
import assign from './assign.js';
import config from '../config.js';
import { to, ensure } from './tools.js';
import { constant } from '@dfsj/utils';

function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function renderAltitudeMark(params, api, current) {
  var scale = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 1;
  var p1 = api.coord([api.value(0), api.value(1)]);
  var p2 = api.coord([api.value(2), api.value(3)]);
  var p3 = api.coord([api.value(4), api.value(5)]);
  var symbol = api.value(7),
    stage = api.value(1);
  var text = "".concat(api.value(6), "\uFF1A").concat(to(api.value(1)), "\uFF08m\uFF09");
  var color = current >= stage && config.indexColor[api.value(8)] || config.markColor.safe;
  var shadow = 'rgba(51,51,51,0.8)';
  if (symbol === 'stage') {
    return {
      type: 'group',
      children: [{
        type: 'path',
        shape: {
          pathData: config.icon.stage,
          x: p1[0] - 9 * scale,
          y: p1[1] - 18.5 * scale,
          width: 190 * scale,
          height: 30 * scale
        },
        style: {
          fill: color,
          shadowBlur: 8 * scale,
          shadowColor: 'rgba(255,255,255,0.3)'
        },
        styleEmphasis: {
          shadowBlur: 5 * scale,
          shadowColor: shadow,
          shadowOffsetX: scale,
          shadowOffsetY: 3 * scale
        }
      }, {
        type: 'text',
        style: {
          text: text,
          x: p1[0] + 10 * scale,
          y: p1[1] - 10 * scale,
          fontSize: 11 * scale,
          textFill: '#fff',
          textBorderWidth: 1,
          textBorderRadius: 20 * scale,
          textBackgroundColor: color,
          textLineWidth: 100 * scale,
          textVerticalAlign: 'middle',
          textAlign: 'left',
          textPadding: [4 * scale, 5 * scale, 2 * scale, 5 * scale]
        },
        styleEmphasis: {
          fontSize: 12 * scale,
          textBoxShadowColor: shadow,
          textBoxShadowBlur: 10 * scale,
          textBoxShadowOffsetX: scale,
          textBoxShadowOffsetY: 3 * scale,
          textBackgroundColor: '#fff',
          textFill: color
        }
      }]
    };
  }
  if (symbol === 'altitude') {
    return {
      type: 'group',
      children: [{
        type: 'path',
        shape: {
          pathData: config.icon.altitude,
          x: p1[0] - 9 * scale,
          y: p1[1] - 23 * scale,
          width: 190 * scale,
          height: 30 * scale
        },
        style: {
          fill: color,
          shadowBlur: 8 * scale,
          shadowColor: 'rgba(255,255,255,0.3)'
        },
        styleEmphasis: {
          shadowBlur: 2 * scale,
          shadowColor: shadow,
          shadowOffsetX: scale,
          shadowOffsetY: 3 * scale
        }
      }, {
        type: 'text',
        style: {
          text: text,
          x: p1[0] + 10 * scale,
          y: p1[1] - 10 * scale,
          fontSize: 11 * scale,
          textFill: '#fff',
          textBorderWidth: 1,
          textBorderRadius: 20 * scale,
          textBackgroundColor: color,
          textLineWidth: 100 * scale,
          textVerticalAlign: 'middle',
          textAlign: 'left',
          textPadding: [4 * scale, 5 * scale, 2 * scale, 5 * scale]
        },
        styleEmphasis: {
          fontSize: 12 * scale,
          textBoxShadowColor: shadow,
          textBoxShadowBlur: 10 * scale,
          textBoxShadowOffsetX: scale,
          textBoxShadowOffsetY: 3 * scale,
          textBackgroundColor: '#fff',
          textFill: color
        }
      }]
    };
  }
  if (symbol === 'exception') {
    color = config.markColor["4"];
    return {
      type: 'group',
      children: [{
        type: 'path',
        shape: {
          pathData: config.icon.exception,
          x: p1[0],
          y: p1[1],
          width: 10,
          height: 30
        },
        style: {
          fill: color,
          shadowBlur: 8 * scale,
          shadowColor: 'rgba(255,255,255,0.3)'
        },
        styleEmphasis: {
          shadowBlur: 2 * scale,
          shadowColor: shadow,
          shadowOffsetX: scale,
          shadowOffsetY: 3 * scale
        }
      }]
    };
  }
  return {
    type: 'group',
    children: [{
      z2: 5,
      type: 'circle',
      shape: {
        cx: p1[0],
        cy: p1[1],
        r: 4 * scale
      },
      style: {
        fill: '#fff',
        stroke: color
      }
    }, {
      z2: 4,
      type: 'polyline',
      shape: {
        points: [p1, p2, p3]
      },
      style: {
        fill: null,
        lineWidth: 2 * scale,
        stroke: color,
        shadowBlur: 8 * scale,
        shadowColor: '#fff'
      },
      styleEmphasis: {
        shadowBlur: 2 * scale,
        shadowColor: shadow,
        shadowOffsetX: scale,
        shadowOffsetY: 3 * scale
      }
    }, {
      type: 'text',
      style: {
        text: text,
        x: p3[0],
        y: p3[1] - 12 * scale,
        fontSize: 13 * scale,
        textFill: '#fff',
        textBorderWidth: 1,
        textBorderRadius: 20 * scale,
        textBackgroundColor: color,
        textLineWidth: 100 * scale,
        textAlign: 'right',
        textPadding: [6 * scale, 10 * scale, 3 * scale, 10 * scale]
      },
      styleEmphasis: {
        textBoxShadowColor: shadow,
        textBoxShadowBlur: 10 * scale,
        textBoxShadowOffsetX: scale,
        textBoxShadowOffsetY: 3 * scale,
        textBackgroundColor: '#fff',
        textFill: color
      }
    }]
  };
}
function toMarks(value, optioned, mark) {
  var _value$indices, _value$areas;
  var indices = ((_value$indices = value.indices) === null || _value$indices === void 0 ? void 0 : _value$indices.reduce(function (p, e) {
    return p.concat(e.indices.filter(function (idx) {
      var color = idx.color || config.indexColor[idx.level];
      var yAxisIndex = optioned.yAxis.findIndex(function (x) {
        return x && x.$layout & e.code;
      });
      if (yAxisIndex > -1) {
        return assign({
          color: color,
          yAxisIndex: yAxisIndex,
          shape: 'symbol',
          name: idx.label,
          value: idx.value,
          label: {
            text: "".concat(idx.label, "\uFF1A").concat(idx.value) + (e.unit ? "\uFF08".concat(e.unit, "\uFF09") : ''),
            textBackgroundColor: color
          },
          itemStyle: {
            fill: color
          }
        }, mark.shape.symbol, mark.side[yAxisIndex]);
      }
    }));
  }, [])) || constant.EMPTY_ARRAY;
  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]
      }
    }, mark.shape.rect);
  })) || constant.EMPTY_ARRAY;
  var splits = ensure(value.split).reduce(function (accept, split) {
    return accept.concat([assign({
      shape: 'line',
      value: split
    }, mark.shape.line), assign({
      shape: 'text',
      value: split,
      label: {
        text: split,
        offset: [0, -15]
      }
    }, mark.shape.text)]);
  }, []);
  return indices.concat(areas).concat(splits);
}
function toIndices(value, optioned, yAxisCodes) {
  var _value$indices2;
  return (_value$indices2 = value.indices) === null || _value$indices2 === void 0 ? void 0 : _value$indices2.reduce(function (accept, item) {
    return accept.concat(item.indices.map(function (idx) {
      var xAxisIndex = yAxisCodes === null || yAxisCodes === void 0 ? void 0 : yAxisCodes.findIndex(function (v) {
        return v & item.code;
      });
      var yAxisIndex = optioned.yAxis.findIndex(function (x) {
        return x && !!(x.$layout & item.code);
      });
      var enabled = value.dimensions.filter(function (d) {
        return d.code === item.code;
      }).some(function (d) {
        var index = value.dimensions.indexOf(d);
        return value.source.some(function (row) {
          return row[index] != null;
        });
      });
      if (yAxisIndex > -1 && enabled) {
        return toIndex(idx, xAxisIndex, yAxisIndex);
      }
    })).filter(function (e) {
      return !!e;
    });
  }, []);
}
function toIndex(item) {
  var xAxisIndex = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
  var yAxisIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var color = item.color;
  if (color == null) {
    color = config.markColor[item.level] || config.markColor.safe;
  }
  return {
    z: 10,
    type: 'mark',
    name: item.label,
    color: color,
    selected: item.light,
    data: [{
      xAxisIndex: xAxisIndex,
      yAxisIndex: yAxisIndex,
      shape: 'index',
      value: item.value,
      gridIndex: [yAxisIndex > 1 ? 1 : 0],
      label: {
        text: "".concat(item.label, "\uFF1A").concat(item.value) + (item.unit ? "\uFF08".concat(item.unit, "\uFF09") : ''),
        fill: '#fff',
        offset: [5 * (yAxisIndex % 2 ? -1 : 1), -5],
        align: yAxisIndex % 2 ? 'right' : 'left',
        padding: [4, 5, 2, 10],
        verticalAlign: 'bottom',
        backgroundColor: color,
        borderWidth: 1,
        borderRadius: 20
      },
      lineStyle: _objectSpread({
        stroke: color
      }, config.mark.shape.index.lineStyle)
    }]
  };
}

export { renderAltitudeMark, toIndex, toIndices, toMarks };
