import { defineComponent, toRefs, reactive, watch, onUnmounted, openBlock, createElementBlock, normalizeStyle, unref, Fragment, renderList, createElementVNode, toDisplayString, pushScopeId, popScopeId } from "vue";
import "./index.vue_vue_type_style_index_0_scoped_true_lang.mjs";
import _export_sfc from "../../../../_virtual/plugin-vue_export-helper.mjs";
const _withScopeId = (n) => (pushScopeId("data-v-3c40f68e"), n = n(), popScopeId(), n);
const _hoisted_1 = { class: "ranking-info" };
const _hoisted_2 = ["innerHTML"];
const _hoisted_3 = /* @__PURE__ */ _withScopeId(() => /* @__PURE__ */ createElementVNode("div", { class: "shine" }, null, -1));
const _hoisted_4 = [
  _hoisted_3
];
const __default__ = defineComponent({ name: "ETableList" });
const _sfc_main = /* @__PURE__ */ defineComponent({
  ...__default__,
  props: {
    chartConfig: {
      type: Object,
      required: true
    }
  },
  setup(__props) {
    const props = __props;
    const { w, h } = toRefs(props.chartConfig.attr);
    const {
      rowNum,
      unit,
      color,
      textColor,
      borderColor,
      indexFontSize,
      leftFontSize,
      rightFontSize
    } = toRefs(props.chartConfig.option);
    const status = reactive({
      mergedConfig: props.chartConfig.option,
      rowsData: [],
      rows: [
        {
          scroll: 0,
          ranking: 1,
          name: "",
          value: "",
          percent: 0
        }
      ],
      heights: [0],
      animationIndex: 0,
      animationHandler: 0,
      updater: 0,
      avgHeight: 0
    });
    const calcRowsData = () => {
      let { dataset, rowNum: rowNum2, sort } = status.mergedConfig;
      sort && dataset.sort(({ value: a }, { value: b }) => {
        if (a > b)
          return -1;
        if (a < b)
          return 1;
        if (a === b)
          return 0;
      });
      const value = dataset.map(({ value: value2 }) => value2);
      const min = Math.min(...value) || 0;
      const minAbs = Math.abs(min);
      const max = Math.max(...value) || 0;
      const total = max + minAbs;
      dataset = dataset.map((row, i) => ({
        ...row,
        ranking: i + 1,
        percent: (row.value + minAbs) / total * 100
      }));
      const rowLength = dataset.length;
      if (rowLength > rowNum2 && rowLength < 2 * rowNum2) {
        dataset = [...dataset, ...dataset];
      }
      dataset = dataset.map((d, i) => ({ ...d, scroll: i }));
      status.rowsData = dataset;
      status.rows = dataset;
    };
    const calcHeights = (onresize = false) => {
      const { rowNum: rowNum2, dataset } = status.mergedConfig;
      const avgHeight = h.value / rowNum2;
      status.avgHeight = avgHeight;
      if (!onresize)
        status.heights = new Array(dataset.length).fill(avgHeight);
    };
    const animation = async (start = false) => {
      let { avgHeight, animationIndex, mergedConfig, rowsData, updater } = status;
      const { waitTime, carousel, rowNum: rowNum2 } = mergedConfig;
      const rowLength = rowsData.length;
      if (rowNum2 >= rowLength)
        return;
      if (start) {
        await new Promise((resolve) => setTimeout(resolve, waitTime));
        if (updater !== status.updater)
          return;
      }
      const animationNum = carousel === "single" ? 1 : rowNum2;
      let rows = rowsData.slice(animationIndex);
      rows.push(...rowsData.slice(0, animationIndex));
      status.rows = rows.slice(0, rowNum2 + 1);
      status.heights = new Array(rowLength).fill(avgHeight);
      await new Promise((resolve) => setTimeout(resolve, 300));
      if (updater !== status.updater)
        return;
      status.heights.splice(0, animationNum, ...new Array(animationNum).fill(0));
      animationIndex += animationNum;
      const back = animationIndex - rowLength;
      if (back >= 0)
        animationIndex = back;
      status.animationIndex = animationIndex;
      status.animationHandler = setTimeout(animation, waitTime * 1e3 - 300);
    };
    const stopAnimation = () => {
      status.updater = (status.updater + 1) % 999999;
      if (!status.animationHandler)
        return;
      clearTimeout(status.animationHandler);
    };
    const onRestart = async () => {
      try {
        if (!status.mergedConfig)
          return;
        let { dataset, rowNum: rowNum2, sort } = status.mergedConfig;
        stopAnimation();
        calcRowsData();
        let flag = true;
        if (dataset.length <= rowNum2) {
          flag = false;
        }
        calcHeights(flag);
        animation(flag);
      } catch (error) {
        console.error(error);
      }
    };
    onRestart();
    watch(
      () => w.value,
      () => {
        onRestart();
      }
    );
    watch(
      () => h.value,
      () => {
        onRestart();
      }
    );
    watch(
      () => rowNum.value,
      () => {
        onRestart();
      }
    );
    watch(
      () => props.chartConfig.option.dataset,
      () => {
        onRestart();
      },
      {
        deep: false
      }
    );
    onUnmounted(() => {
      stopAnimation();
    });
    return (_ctx, _cache) => {
      return openBlock(), createElementBlock("div", {
        class: "go-tables-rank",
        style: normalizeStyle(`color: ${unref(textColor)}`)
      }, [
        (openBlock(true), createElementBlock(Fragment, null, renderList(status.rows, (item, i) => {
          return openBlock(), createElementBlock("div", {
            class: "row-item",
            key: item.toString() + item.scroll,
            style: normalizeStyle(`height: ${status.heights[i]}px;`)
          }, [
            createElementVNode("div", _hoisted_1, [
              createElementVNode("div", {
                class: "rank",
                style: normalizeStyle(`color: ${unref(color)};font-size: ${unref(indexFontSize)}px`)
              }, " No." + toDisplayString(item.ranking), 5),
              createElementVNode("div", {
                class: "info-name",
                style: normalizeStyle(`font-size: ${unref(leftFontSize)}px`),
                innerHTML: item.name
              }, null, 12, _hoisted_2),
              createElementVNode("div", {
                class: "ranking-value",
                style: normalizeStyle(`color: ${unref(textColor)};font-size: ${unref(rightFontSize)}px`)
              }, toDisplayString(status.mergedConfig.valueFormatter ? status.mergedConfig.valueFormatter(item) : item.value) + " " + toDisplayString(unref(unit)), 5)
            ]),
            createElementVNode("div", {
              class: "ranking-column",
              style: normalizeStyle(`border-color: ${unref(borderColor)}`)
            }, [
              createElementVNode("div", {
                class: "inside-column",
                style: normalizeStyle(`width: ${item.percent}%;background-color: ${unref(color)}`)
              }, _hoisted_4, 4)
            ], 4)
          ], 4);
        }), 128))
      ], 4);
    };
  }
});
var _ETableFix = /* @__PURE__ */ _export_sfc(_sfc_main, [["__scopeId", "data-v-3c40f68e"]]);
export { _ETableFix as default };
