/**
 * tdesign v0.39.1-alpha.1
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var utils_event = require('../utils/event.js');
var config = require('../config.js');
var swiper_props = require('./props.js');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/camelCase');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty__default["default"](target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
var name = "".concat(config.prefix, "-swiper");
var _Swiper = Vue__default["default"].extend({
  name: "TSwiper",
  props: _objectSpread({}, swiper_props["default"]),
  data: function data() {
    return {
      visible: true,
      index: 0,
      timeoutHandler: null
    };
  },
  computed: {
    items: function items() {
      return (this.$slots["default"] || []).filter(function (child) {
        var node = child;
        return node.componentOptions && node.componentOptions.tag === "".concat(config.prefix, "-swiper-item");
      });
    }
  },
  watch: {
    interval: {
      handler: function handler() {
        this.swiperTo(this.index, "");
      },
      immediate: true
    }
  },
  render: function render() {
    var h = arguments[0];
    var swiperClass = ["".concat(name), _defineProperty__default["default"]({}, "".concat(config.prefix, "-is-hidden"), !this.visible)];
    return h("div", {
      "class": swiperClass,
      "on": {
        "mouseenter": this.clearTimer,
        "mouseleave": this.setTimer
      }
    }, [this.renderContent(), this.renderTrigger()]);
  },
  methods: {
    renderContent: function renderContent() {
      var h = this.$createElement;
      var wraperStyles = {};

      if (this.direction === "vertical") {
        wraperStyles = {
          height: "".concat(this.items.length * 100, "%"),
          transform: "translate(0,".concat(-this.index * 100 / this.items.length, "%)"),
          transition: "transform ".concat(this.duration / 1e3, "s")
        };
      } else {
        wraperStyles = {
          width: "".concat(this.items.length * 100, "%"),
          transform: "translate(".concat(-this.index * 100 / this.items.length, "%,0)"),
          transition: "transform ".concat(this.duration / 1e3, "s")
        };
      }

      return h("div", {
        "class": "".concat(name, "__content")
      }, [h("div", {
        "class": "".concat(name, "__swiper-wrap--").concat(this.direction),
        "style": wraperStyles
      }, [this.items])]);
    },
    renderTrigger: function renderTrigger() {
      var _this = this;

      var h = this.$createElement;
      var index = this.index % this.items.length;
      return h("ul", {
        "class": "t-swiper__trigger-wrap"
      }, [this.items.map(function (_, i) {
        return h("li", {
          "class": i === index ? "t-swiper__trigger--active" : "",
          "on": {
            "click": function click() {
              return _this.swiperTo(i, "touch");
            }
          }
        });
      })]);
    },
    swiperToNext: function swiperToNext(source) {
      var number = this.index + 1;
      this.swiperTo(number, source);

      if (source) {
        utils_event.emitEvent(this, "change", number, {
          source: source
        });
      }
    },
    swiperTo: function swiperTo(index, source) {
      var _this2 = this;

      var findIndex = this.items.length === 0 ? 0 : index % this.items.length;

      if (this.timeoutHandler) {
        this.clearTimer();
      }

      this.index = findIndex;

      if (this.interval > 0) {
        this.timeoutHandler = setTimeout(function () {
          _this2.swiperToNext(source);
        }, this.interval);
      }
    },
    setTimer: function setTimer() {
      var _this3 = this;

      if (this.interval > 0) {
        this.timeoutHandler = Number(setTimeout(function () {
          _this3.clearTimer();

          _this3.swiperToNext("autoplay");
        }, this.interval));
      }
    },
    clearTimer: function clearTimer() {
      clearTimeout(this.timeoutHandler);
      this.timeoutHandler = null;
    }
  }
});

exports["default"] = _Swiper;
//# sourceMappingURL=swiper.js.map
