function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { ajax, BizGlobalDataContext } from '@zgfe/business-lib';
import { getCondition } from '@zgfe/business-lib/es/attrConditions';
import { OperateTypes } from '@zgfe/business-lib/es/attrConditions/types';
import { Spin } from 'antd';
import * as echarts from 'echarts';
import moment from 'moment';
import React, { useContext, useEffect, useState } from 'react';
import apis from "../apis";
import VisualContext from "./context";
var EventTrend = function EventTrend() {
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    chart = _useState2[0],
    setChart = _useState2[1];
  var _useContext = useContext(VisualContext),
    currentElement = _useContext.currentElement,
    currentPage = _useContext.currentPage;
  var _useContext2 = useContext(BizGlobalDataContext),
    currentApp = _useContext2.currentApp,
    eventNameMap = _useContext2.eventNameMap;
  var _useState3 = useState(false),
    _useState4 = _slicedToArray(_useState3, 2),
    loading = _useState4[0],
    setLoading = _useState4[1];
  var _useState5 = useState(),
    _useState6 = _slicedToArray(_useState5, 2),
    option = _useState6[0],
    setOption = _useState6[1];
  var _useState7 = useState(false),
    _useState8 = _slicedToArray(_useState7, 2),
    empty = _useState8[0],
    setEmpty = _useState8[1];
  function initChart() {
    var _chart = echarts.init(document.getElementById('id'));
    setChart(_chart);
  }
  function getOption(source) {
    var data = source === null || source === void 0 ? void 0 : source.app_data;
    return {
      grid: {
        top: 22,
        bottom: 22
      },
      tooltip: {
        trigger: 'axis'
      },
      xAxis: {
        type: 'category',
        data: (data === null || data === void 0 ? void 0 : data.x_axis) || [],
        axisLabel: {
          formatter: function formatter(value) {
            return moment(value).format('MM/DD');
          }
        },
        axisTick: {
          show: false
        }
      },
      yAxis: {
        type: 'value',
        splitNumber: 3,
        splitLine: {
          lineStyle: {
            type: 'dashed'
          }
        }
      },
      series: [{
        data: (data === null || data === void 0 ? void 0 : data.series[0].values) || [],
        type: 'line',
        lineStyle: {
          color: '#165DFF'
        },
        symbol: 'circle'
      }]
    };
  }
  function getFilterCondition(event) {
    return {
      eventCondition: {
        eventSingleConditions: [{
          attrConditions: event.attrList.filter(function (attr) {
            return (currentElement === null || currentElement === void 0 ? void 0 : currentElement.viewName) && attr.label === 'element_content';
          }).map(function (attr) {
            return getCondition(attr, OperateTypes.Equal, [currentElement.viewName]);
          }),
          environmentConditions: [{
            attrId: 0,
            name: 'current_url',
            type: 1,
            category: 'fixed',
            operator: OperateTypes.Equal,
            text: '当前URL',
            params: [currentPage]
          }],
          eventId: event.id,
          eventName: event.name,
          happenTime: {
            name: 'happenTime',
            operator: 'between',
            params: ['relative', '7', '0', 'day'],
            type: 3
          },
          isNew: true
        }],
        operator: 'and'
      }
    };
  }
  function loadData() {
    var event = (eventNameMap || {})[currentElement ? 'click' : 'pv'];
    if (!event) {
      // chart显示无数据
      setEmpty(true);
      return;
    }
    setLoading(true);
    ajax(apis.queryTrend, {
      method: 'post',
      data: {
        appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId,
        platform: 0,
        module: 'event',
        event: event.id,
        time: {
          dimensionDate: 'day',
          beginDate: moment().startOf('day').add(-7, 'day').format('YYYY-MM-DD'),
          endDate: moment().startOf('day').format('YYYY-MM-DD')
        },
        filterCondition: getFilterCondition(event)
      }
    }).then(function (res) {
      if (!res) return;
      if (!res.data.app_data.series.values.length) {
        // chart显示无数据
        setEmpty(true);
        return;
      }
      setEmpty(false);
      initChart();
      setOption(getOption(res.data));
    }).catch(function (err) {
      console.error('获取趋势数据失败', err);
    }).finally(function () {
      setLoading(false);
    });
  }
  useEffect(function () {
    if (!option) return;
    chart === null || chart === void 0 ? void 0 : chart.setOption(option);
  }, [option]);
  useEffect(function () {
    loadData();
    return function () {
      chart === null || chart === void 0 ? void 0 : chart.dispose();
      setChart(undefined);
    };
  }, []);
  useEffect(function () {
    if (!chart) return;
    if (loading) {
      chart.showLoading();
    } else {
      chart.hideLoading();
    }
  }, [loading]);
  if (loading) {
    return /*#__PURE__*/React.createElement(Spin, {
      style: {
        margin: '20px auto',
        display: 'block'
      }
    });
  }
  if (empty) {
    return /*#__PURE__*/React.createElement("div", {
      style: {
        margin: '20px auto',
        display: 'block',
        textAlign: 'center',
        color: '#999'
      }
    }, "\u6682\u65E0\u6570\u636E");
  }
  return /*#__PURE__*/React.createElement("div", {
    id: "id",
    style: {
      width: '312px',
      height: '200px'
    }
  });
};
export default EventTrend;