import * as _vue from "vue";

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

import classNames from '../_util/classNames';
import PropTypes from '../_util/vue-types';
import BaseMixin from '../_util/BaseMixin';
import omit from 'omit.js';
import { cloneElement } from '../_util/vnode';
import { ConfigConsumerProps } from '../config-provider';
import { inject } from 'vue';
import syncWatch from '../_util/syncWatch';

function getNumberArray(num) {
  return num ? num.toString().split('').reverse().map(function (i) {
    var current = Number(i);
    return isNaN(current) ? i : current;
  }) : [];
}

var ScrollNumberProps = {
  prefixCls: PropTypes.string,
  count: PropTypes.any,
  component: PropTypes.string,
  title: PropTypes.oneOfType([PropTypes.number, PropTypes.string, null]),
  displayComponent: PropTypes.any,
  onAnimated: PropTypes.func
};
export default {
  name: 'ScrollNumber',
  mixins: [BaseMixin],
  inheritAttrs: false,
  props: ScrollNumberProps,
  setup: function setup() {
    return {
      configProvider: inject('configProvider', ConfigConsumerProps)
    };
  },
  data: function data() {
    this.lastCount = undefined;
    return {
      animateStarted: true,
      sCount: this.count
    };
  },
  watch: {
    count: syncWatch(function () {
      this.lastCount = this.sCount;
      this.setState({
        animateStarted: true
      });
    })
  },
  updated: function updated() {
    var _this = this;

    var animateStarted = this.animateStarted,
        count = this.count;

    if (animateStarted) {
      this.clearTimeout(); // Let browser has time to reset the scroller before actually
      // performing the transition.

      this.timeout = setTimeout(function () {
        _this.setState({
          animateStarted: false,
          sCount: count
        }, _this.handleAnimated);
      });
    }
  },
  beforeUnmount: function beforeUnmount() {
    this.clearTimeout();
  },
  methods: {
    clearTimeout: function (_clearTimeout) {
      function clearTimeout() {
        return _clearTimeout.apply(this, arguments);
      }

      clearTimeout.toString = function () {
        return _clearTimeout.toString();
      };

      return clearTimeout;
    }(function () {
      if (this.timeout) {
        clearTimeout(this.timeout);
        this.timeout = undefined;
      }
    }),
    getPositionByNum: function getPositionByNum(num, i) {
      var sCount = this.sCount;
      var currentCount = Math.abs(Number(sCount));
      var lastCount = Math.abs(Number(this.lastCount));
      var currentDigit = Math.abs(getNumberArray(sCount)[i]);
      var lastDigit = Math.abs(getNumberArray(this.lastCount)[i]);

      if (this.animateStarted) {
        return 10 + num;
      } // 同方向则在同一侧切换数字


      if (currentCount > lastCount) {
        if (currentDigit >= lastDigit) {
          return 10 + num;
        }

        return 20 + num;
      }

      if (currentDigit <= lastDigit) {
        return 10 + num;
      }

      return num;
    },
    handleAnimated: function handleAnimated() {
      this.$emit('animated');
    },
    renderNumberList: function renderNumberList(position, className) {
      var childrenToReturn = [];

      for (var i = 0; i < 30; i++) {
        childrenToReturn.push(_vue.createVNode("p", {
          "key": i.toString(),
          "class": classNames(className, {
            current: position === i
          })
        }, [i % 10]));
      }

      return childrenToReturn;
    },
    renderCurrentNumber: function renderCurrentNumber(prefixCls, num, i) {
      if (typeof num === 'number') {
        var position = this.getPositionByNum(num, i);
        var removeTransition = this.animateStarted || getNumberArray(this.lastCount)[i] === undefined;
        var style = {
          transition: removeTransition ? 'none' : undefined,
          msTransform: "translateY(".concat(-position * 100, "%)"),
          WebkitTransform: "translateY(".concat(-position * 100, "%)"),
          transform: "translateY(".concat(-position * 100, "%)")
        };
        return _vue.createVNode("span", {
          "class": "".concat(prefixCls, "-only"),
          "style": style,
          "key": i
        }, [this.renderNumberList(position, "".concat(prefixCls, "-only-unit"))]);
      }

      return _vue.createVNode("span", {
        "key": "symbol",
        "class": "".concat(prefixCls, "-symbol")
      }, [num]);
    },
    renderNumberElement: function renderNumberElement(prefixCls) {
      var _this2 = this;

      var sCount = this.sCount;

      if (sCount && Number(sCount) % 1 === 0) {
        return getNumberArray(sCount).map(function (num, i) {
          return _this2.renderCurrentNumber(prefixCls, num, i);
        }).reverse();
      }

      return sCount;
    }
  },
  render: function render() {
    var _this3 = this;

    var customizePrefixCls = this.prefixCls,
        title = this.title,
        _this$component = this.component,
        Tag = _this$component === void 0 ? 'sup' : _this$component,
        displayComponent = this.displayComponent;
    var getPrefixCls = this.configProvider.getPrefixCls;
    var prefixCls = getPrefixCls('scroll-number', customizePrefixCls);
    var _this$$attrs = this.$attrs,
        className = _this$$attrs.class,
        _this$$attrs$style = _this$$attrs.style,
        style = _this$$attrs$style === void 0 ? {} : _this$$attrs$style;

    if (displayComponent) {
      return cloneElement(displayComponent, {
        class: classNames("".concat(prefixCls, "-custom-component"), displayComponent.props && displayComponent.props.class)
      });
    } // fix https://fb.me/react-unknown-prop


    var restProps = omit(_objectSpread(_objectSpread({}, this.$props), this.$attrs), ['count', 'onAnimated', 'component', 'prefixCls', 'displayComponent']);

    var tempStyle = _objectSpread({}, style);

    var newProps = _objectSpread(_objectSpread({}, restProps), {}, {
      title: title,
      style: tempStyle,
      class: classNames(prefixCls, className)
    }); // allow specify the border
    // mock border-color by box-shadow for compatible with old usage:
    // <Badge count={4} style={{ backgroundColor: '#fff', color: '#999', borderColor: '#d9d9d9' }} />


    if (style && style.borderColor) {
      newProps.style.boxShadow = "0 0 0 1px ".concat(style.borderColor, " inset");
    }

    return _vue.createVNode(Tag, newProps, {
      default: function _default() {
        return [_this3.renderNumberElement(prefixCls)];
      }
    });
  }
};