/**
 * tdesign v0.46.4
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

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

var helper = require('../_chunks/dep-d7a1e02c.js');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var Vue = require('vue');
var tdesignIconsVue = require('tdesign-icons-vue');
var kebabCase = require('lodash/kebabCase');
var swiper_props = require('./props.js');
var swiper_swiperItem = require('./swiper-item.js');
var hooks_renderTnode = require('../hooks/render-tnode.js');
var utils_event = require('../utils/event.js');
var configProvider_configReceiver = require('../config-provider/config-receiver.js');
var utils_mixins = require('../utils/mixins.js');
require('@vue/composition-api');
require('lodash/isEmpty');
require('lodash/isString');
require('lodash/isFunction');
require('lodash/isObject');
require('lodash/camelCase');
require('../utils/helper.js');
require('@babel/runtime/helpers/objectWithoutProperties');
require('@babel/runtime/helpers/slicedToArray');
require('lodash/mergeWith');
require('../config-provider/context.js');
require('lodash/merge');
require('../_common/js/global-config/default-config.js');
require('../_common/js/global-config/locale/zh_CN.js');

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);
var kebabCase__default = /*#__PURE__*/_interopDefaultLegacy(kebabCase);

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 classPrefixMixins = configProvider_configReceiver.getClassPrefixMixins("swiper");
var defaultNavigation = {
  placement: "inside",
  showSlideBtn: "always",
  size: "medium",
  type: "bars"
};
var _Swiper = utils_mixins["default"](Vue__default["default"], classPrefixMixins, configProvider_configReceiver.getGlobalIconMixins()).extend(_objectSpread(_objectSpread({
  name: "TSwiper",
  components: {
    TSwiperItem: swiper_swiperItem["default"]
  }
}, {
  swiperTimer: 0,
  swiperSwitchingTimer: 0
}), {}, {
  props: _objectSpread({}, swiper_props["default"]),
  data: function data() {
    return {
      currentIndex: 0,
      isHovering: false,
      isSwitching: false,
      swiperItemList: [],
      showArrow: false
    };
  },
  computed: {
    swiperItemLength: function swiperItemLength() {
      return this.swiperItemList.length;
    },
    navigationConfig: function navigationConfig() {
      return _objectSpread(_objectSpread({}, defaultNavigation), hooks_renderTnode.isVNode(this.navigation) ? {} : this.navigation);
    },
    isEnd: function isEnd() {
      if (this.type === "card") {
        return !this.loop && this.currentIndex + 1 >= this.swiperItemLength;
      }

      return !this.loop && this.currentIndex + 2 >= this.swiperItemLength;
    },
    propsToUpdateSetTimer: function propsToUpdateSetTimer() {
      return [this.autoplay, this.currentIndex, this.duration, this.interval];
    },
    swiperWrapClass: function swiperWrapClass() {
      var _ref;

      return _ref = {}, _defineProperty__default["default"](_ref, "".concat(this.componentName, "__wrap"), true), _defineProperty__default["default"](_ref, "".concat(this.componentName, "--inside"), this.navigationConfig.placement === "inside"), _defineProperty__default["default"](_ref, "".concat(this.componentName, "--outside"), this.navigationConfig.placement === "outside"), _defineProperty__default["default"](_ref, "".concat(this.componentName, "--vertical"), this.direction === "vertical"), _defineProperty__default["default"](_ref, "".concat(this.componentName, "--large"), this.navigationConfig.size === "large"), _defineProperty__default["default"](_ref, "".concat(this.componentName, "--small"), this.navigationConfig.size === "small"), _ref;
    },
    containerStyle: function containerStyle() {
      var offsetHeight = this.height ? "".concat(this.height, "px") : "".concat(this.getWrapAttribute("offsetHeight"), "px");

      if (this.type === "card" || this.animation === "fade") {
        return {
          height: offsetHeight
        };
      }

      if (this.animation === "slide") {
        if (this.direction === "vertical") {
          return {
            height: offsetHeight,
            transform: "translate3d(0, -".concat(this.currentIndex * 100, "%, 0px)"),
            transition: this.isSwitching ? "transform ".concat(this.duration / 1e3, "s ease") : ""
          };
        }

        return {
          msTransform: "translate3d(-".concat(this.currentIndex * 100, "%, 0px, 0px)"),
          WebkitTransform: "translate3d(-".concat(this.currentIndex * 100, "%, 0px, 0px)"),
          transform: "translate3d(-".concat(this.currentIndex * 100, "%, 0px, 0px)"),
          transition: this.isSwitching ? "transform ".concat(this.duration / 1e3, "s ease") : ""
        };
      }

      return {};
    },
    swiperItems: function swiperItems() {
      var _this = this;

      var h = this.$createElement;
      return this.swiperItemList.map(function (swiperItem, index) {
        return h(swiper_swiperItem["default"], helper.helper([{
          "attrs": {
            "index": index,
            "currentIndex": _this.currentIndex,
            "isSwitching": _this.isSwitching,
            "getWrapAttribute": _this.getWrapAttribute,
            "swiperItemLength": _this.swiperItemLength
          }
        }, {
          "props": _objectSpread(_objectSpread({}, _this.$props), swiperItem.propsData)
        }]), [swiperItem.children]);
      });
    }
  },
  watch: {
    propsToUpdateSetTimer: function propsToUpdateSetTimer() {
      this.setTimer();
    },
    isSwitching: function isSwitching() {
      var _this2 = this;

      if (this.isSwitching) {
        if (this.swiperSwitchingTimer) clearTimeout(this.swiperSwitchingTimer);
        this.swiperSwitchingTimer = setTimeout(function () {
          _this2.isSwitching = false;
          _this2.swiperSwitchingTimer = 0;

          if (_this2.isEnd) {
            _this2.clearTimer();
          }
        }, this.duration + 50);
      }
    },
    current: function current() {
      this.swiperTo(this.current, {
        source: "autoplay"
      });
    }
  },
  mounted: function mounted() {
    this.updateSwiperItems();
    this.setTimer();
    this.showArrow = this.navigationConfig.showSlideBtn === "always";
  },
  updated: function updated() {
    var _this3 = this;

    this.$nextTick(function () {
      _this3.updateSwiperItems();
    });
  },
  methods: {
    updateSwiperItems: function updateSwiperItems() {
      var _this$$scopedSlots$de,
          _this$$scopedSlots,
          _this4 = this;

      var originalChildren = ((_this$$scopedSlots$de = (_this$$scopedSlots = this.$scopedSlots)["default"]) === null || _this$$scopedSlots$de === void 0 ? void 0 : _this$$scopedSlots$de.call(_this$$scopedSlots, {})) || [];
      var swiperItemList = originalChildren.map(function (swiper) {
        return swiper.componentOptions;
      }).filter(function (swiper) {
        return kebabCase__default["default"](swiper === null || swiper === void 0 ? void 0 : swiper.tag).endsWith("".concat(_this4.componentName, "-item"));
      });
      var isUnchange = swiperItemList.length === this.swiperItemList.length && this.swiperItemList.every(function (swiperItem, index) {
        return swiperItem === swiperItemList[index];
      });
      if (isUnchange) return;
      this.swiperItemList = swiperItemList;
    },
    setTimer: function setTimer() {
      var _this5 = this;

      if (this.autoplay && this.interval > 0) {
        this.clearTimer();
        this.swiperTimer = setTimeout(function () {
          _this5.swiperTo(_this5.currentIndex + 1, {
            source: "autoplay"
          });
        }, this.currentIndex === 0 ? this.interval - (this.duration + 50) : this.interval);
      }
    },
    clearTimer: function clearTimer() {
      if (this.swiperTimer) {
        clearTimeout(this.swiperTimer);
        this.swiperTimer = 0;
      }
    },
    onMouseEnter: function onMouseEnter() {
      this.isHovering = true;

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

      if (this.navigationConfig.showSlideBtn === "hover") {
        this.showArrow = true;
      }
    },
    onMouseLeave: function onMouseLeave() {
      this.isHovering = false;

      if (!this.isEnd) {
        this.setTimer();
      }

      if (this.navigationConfig.showSlideBtn === "hover") {
        this.showArrow = false;
      }
    },
    onMouseEnterNavigationItem: function onMouseEnterNavigationItem(i) {
      this.swiperTo(i, {
        source: "hover"
      });
    },
    onClickNavigationItem: function onClickNavigationItem(i) {
      this.swiperTo(i, {
        source: "click"
      });
    },
    swiperTo: function swiperTo(index, context) {
      var targetIndex = index % this.swiperItemLength;
      utils_event.emitEvent(this, "change", targetIndex, context);
      this.isSwitching = true;
      this.currentIndex = targetIndex;
    },
    goNext: function goNext(context) {
      if (this.isSwitching) return;

      if (this.type === "card") {
        return this.swiperTo(this.currentIndex + 1 >= this.swiperItemLength ? 0 : this.currentIndex + 1, context);
      }

      return this.swiperTo(this.currentIndex + 1, context);
    },
    goPrevious: function goPrevious(context) {
      if (this.isSwitching) return;

      if (this.currentIndex - 1 < 0) {
        return this.swiperTo(this.swiperItemLength - 1, context);
      }

      return this.swiperTo(this.currentIndex - 1, context);
    },
    getWrapAttribute: function getWrapAttribute(attr) {
      var _this$$refs$swiperWra, _this$$refs$swiperWra2;

      return (_this$$refs$swiperWra = this.$refs.swiperWrap) === null || _this$$refs$swiperWra === void 0 ? void 0 : (_this$$refs$swiperWra2 = _this$$refs$swiperWra.parentNode) === null || _this$$refs$swiperWra2 === void 0 ? void 0 : _this$$refs$swiperWra2[attr];
    },
    renderPagination: function renderPagination() {
      var _this6 = this;

      var h = this.$createElement;
      var fractionIndex = this.currentIndex + 1 > this.swiperItemLength ? 1 : this.currentIndex + 1;

      var _this$useGlobalIcon = this.useGlobalIcon({
        ChevronLeftIcon: tdesignIconsVue.ChevronLeftIcon,
        ChevronRightIcon: tdesignIconsVue.ChevronRightIcon
      }),
          ChevronLeftIcon = _this$useGlobalIcon.ChevronLeftIcon,
          ChevronRightIcon = _this$useGlobalIcon.ChevronRightIcon;

      return h("div", {
        "class": "".concat(this.componentName, "__arrow")
      }, [h("div", {
        "class": "".concat(this.componentName, "__arrow-left"),
        "on": {
          "click": function click() {
            return _this6.goPrevious({
              source: "click"
            });
          }
        }
      }, [h(ChevronLeftIcon)]), h("div", {
        "class": "".concat(this.componentName, "__navigation-text-fraction")
      }, [fractionIndex, "/", this.swiperItemLength]), h("div", {
        "class": "".concat(this.componentName, "__arrow-right"),
        "on": {
          "click": function click() {
            return _this6.goNext({
              source: "click"
            });
          }
        }
      }, [h(ChevronRightIcon)])]);
    },
    renderArrow: function renderArrow() {
      var _this7 = this;

      var h = this.$createElement;
      if (!this.showArrow) return null;

      var _this$useGlobalIcon2 = this.useGlobalIcon({
        ChevronLeftIcon: tdesignIconsVue.ChevronLeftIcon,
        ChevronRightIcon: tdesignIconsVue.ChevronRightIcon
      }),
          ChevronLeftIcon = _this$useGlobalIcon2.ChevronLeftIcon,
          ChevronRightIcon = _this$useGlobalIcon2.ChevronRightIcon;

      return h("div", {
        "class": ["".concat(this.componentName, "__arrow"), "".concat(this.componentName, "__arrow--default")]
      }, [h("div", {
        "class": "".concat(this.componentName, "__arrow-left"),
        "on": {
          "click": function click() {
            return _this7.goPrevious({
              source: "click"
            });
          }
        }
      }, [h(ChevronLeftIcon)]), h("div", {
        "class": "".concat(this.componentName, "__arrow-right"),
        "on": {
          "click": function click() {
            return _this7.goNext({
              source: "click"
            });
          }
        }
      }, [h(ChevronRightIcon)])]);
    },
    renderNavigation: function renderNavigation() {
      var _this8 = this;

      var h = this.$createElement;
      if (hooks_renderTnode.isVNode(this.navigation)) return this.navigation;

      if (this.navigationConfig.type === "fraction") {
        return h("div", {
          "class": ["".concat(this.componentName, "__navigation"), "".concat(this.componentName, "__navigation--fraction")]
        }, [this.renderPagination()]);
      }

      return h("ul", {
        "class": ["".concat(this.componentName, "__navigation"), _defineProperty__default["default"]({}, "".concat(this.componentName, "__navigation-bars"), this.navigationConfig.type === "bars")]
      }, [this.swiperItemList.map(function (_, i) {
        return h("li", {
          "key": i,
          "class": ["".concat(_this8.componentName, "__navigation-item"), _defineProperty__default["default"]({}, "".concat(_this8.componentName, "-is-active"), i === _this8.currentIndex)],
          "on": {
            "mouseenter": function mouseenter() {
              return _this8.onMouseEnterNavigationItem(i);
            },
            "click": function click() {
              return _this8.onClickNavigationItem(i);
            }
          }
        }, [h("span")]);
      })]);
    },
    renderSwiperItems: function renderSwiperItems() {
      return this.swiperItems;
    }
  },
  render: function render() {
    var _ref4;

    var h = arguments[0];
    return h("div", {
      "class": this.componentName,
      "on": {
        "mouseenter": this.onMouseEnter,
        "mouseLeave": this.onMouseLeave
      },
      "ref": "swiperWrap"
    }, [h("div", {
      "class": this.swiperWrapClass
    }, [h("div", {
      "class": ["".concat(this.componentName, "__content"), (_ref4 = {}, _defineProperty__default["default"](_ref4, "".concat(this.componentName, "-fade"), this.animation === "fade"), _defineProperty__default["default"](_ref4, "".concat(this.componentName, "-card"), this.type === "card"), _ref4)]
    }, [h("div", {
      "class": "".concat(this.componentName, "__container"),
      "style": this.containerStyle
    }, [this.renderSwiperItems()])]), this.renderNavigation(), this.renderArrow()])]);
  }
}));

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