/**
 * ldesign v0.4.6
 * (c) 2023 ldesign
 * @license MIT
 */

import { h as helper } from '../_chunks/dep-c89aafbb.js';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import Vue from 'vue';
import { ChevronLeftIcon, ChevronRightIcon } from '@ldesign/icons-vue';
import kebabCase from 'lodash/kebabCase';
import { isVNode } from '../hooks/render-tnode.js';
import { emitEvent } from '../utils/event.js';
import { getClassPrefixMixins, getGlobalIconMixins } from '../config-provider/config-receiver.js';
import mixins from '../utils/mixins.js';
import _SwiperItem from './swiper-item.js';
import props from './props.js';
import '@vue/composition-api';
import 'lodash/isEmpty';
import 'lodash/isString';
import 'lodash/isFunction';
import 'lodash/isObject';
import 'lodash/camelCase';
import '../utils/helper.js';
import '@babel/runtime/helpers/objectWithoutProperties';
import '@babel/runtime/helpers/slicedToArray';
import 'lodash/mergeWith';
import '../config-provider/context.js';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/zh_CN.js';
import '@babel/runtime/helpers/typeof';
import '../_chunks/dep-be599ec8.js';
import 'dayjs';
import '../_chunks/dep-67ff4010.js';
import '../config-provider/type.js';

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(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 = getClassPrefixMixins("swiper");
var defaultNavigation = {
  placement: "inside",
  showSlideBtn: "always",
  size: "medium",
  type: "bars"
};
var _Swiper = mixins(Vue, classPrefixMixins, getGlobalIconMixins()).extend(_objectSpread(_objectSpread({
  name: "TSwiper",
  components: {
    TSwiperItem: _SwiperItem
  }
}, {
  swiperTimer: 0,
  swiperSwitchingTimer: 0
}), {}, {
  props: _objectSpread({}, props),
  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), 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(_ref, "".concat(this.componentName, "__wrap"), true), _defineProperty(_ref, "".concat(this.componentName, "--inside"), this.navigationConfig.placement === "inside"), _defineProperty(_ref, "".concat(this.componentName, "--outside"), this.navigationConfig.placement === "outside"), _defineProperty(_ref, "".concat(this.componentName, "--vertical"), this.direction === "vertical"), _defineProperty(_ref, "".concat(this.componentName, "--large"), this.navigationConfig.size === "large"), _defineProperty(_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(_SwiperItem, 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(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) {
      if (this.trigger === "hover") this.swiperTo(i, {
        source: "hover"
      });
    },
    onClickNavigationItem: function onClickNavigationItem(i) {
      if (this.trigger === "click") this.swiperTo(i, {
        source: "click"
      });
    },
    swiperTo: function swiperTo(index, context) {
      var targetIndex = index % this.swiperItemLength;
      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: ChevronLeftIcon,
          ChevronRightIcon: ChevronRightIcon
        }),
        ChevronLeftIcon$1 = _this$useGlobalIcon.ChevronLeftIcon,
        ChevronRightIcon$1 = _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$1)]), 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$1)])]);
    },
    renderArrow: function renderArrow() {
      var _this7 = this;
      var h = this.$createElement;
      if (!this.showArrow) return null;
      var _this$useGlobalIcon2 = this.useGlobalIcon({
          ChevronLeftIcon: ChevronLeftIcon,
          ChevronRightIcon: ChevronRightIcon
        }),
        ChevronLeftIcon$1 = _this$useGlobalIcon2.ChevronLeftIcon,
        ChevronRightIcon$1 = _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$1)]), h("div", {
        "class": "".concat(this.componentName, "__arrow-right"),
        "on": {
          "click": function click() {
            return _this7.goNext({
              source: "click"
            });
          }
        }
      }, [h(ChevronRightIcon$1)])]);
    },
    renderNavigation: function renderNavigation() {
      var _this8 = this;
      var h = this.$createElement;
      if (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({}, "".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({}, "".concat(_this8.classPrefix, "-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(_ref4, "".concat(this.componentName, "-fade"), this.animation === "fade"), _defineProperty(_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()])]);
  }
}));

export { _Swiper as default };
//# sourceMappingURL=swiper.js.map
