function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

import { rpx2vw4style as __rpx2vw__ } from "babel-runtime-jsx-style-transform";

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); }

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

import { __rest } from "tslib";
import classNames from 'classnames';
import React, { Component, createElement, createRef } from "react";
import { Text, Video } from "@alifd/meet-react-component-one";
import Icon from '../icon';
import Modal from '../modal';
import { isStrictMiniApp, isStrictWechatMiniProgram, isString, nextTick, px2rpx, windowHeight } from '../utils';
import { guid } from '../utils/hooks';
import { renderToRoot } from '../utils/render';
import View from '../view';
import SliderItem from './item';
import Slider from './slider';
import { getContexts, pauseAll } from './util';
import './styles/index.css';

var SliderWrapper = /*#__PURE__*/function (_Component) {
  _inherits(SliderWrapper, _Component);

  var _super = _createSuper(SliderWrapper);

  function SliderWrapper(props) {
    var _this;

    _classCallCheck(this, SliderWrapper);

    _this = _super.call(this, props);

    _this.handleClick = function (index) {
      if (_this.props.onClick) {
        _this.props.onClick(index);
      }
    };

    _this.handleClose = function () {
      var onClose = _this.props.onClose;

      _this.setState({
        visible: false
      });

      if (onClose) {
        onClose();
      }
    };

    _this.handleChange = function (index) {
      var _this$props = _this.props,
          onChange = _this$props.onChange,
          _this$props$autoContr = _this$props.autoControl,
          autoControl = _this$props$autoContr === void 0 ? true : _this$props$autoContr;
      var nodes = getContexts(_this.itemRefs, _this.state.sliderIdPrefix);

      if (autoControl) {
        pauseAll(nodes);

        if (nodes && nodes[index] && nodes[index].play) {
          nodes[index].play();
        }
      }

      _this.setState({
        curIndex: index
      });

      if (onChange) {
        onChange(index, nodes, _this.itemRefs);
      }
    };

    _this.handleItemClick = function (isImage) {
      if (isImage) {
        _this.handleClose();
      }
    };

    _this.state = {
      visible: true,
      curIndex: props.activeIndex,
      sliderIdPrefix: guid('slider-')
    };
    _this.itemRefs = [];
    return _this;
  }

  _createClass(SliderWrapper, [{
    key: "componentWillMount",
    value: function componentWillMount() {
      var items = this.props.items;

      for (var i = 0; i < items.length; i++) {
        this.itemRefs[i] = /*#__PURE__*/createRef();
      }
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this2 = this;

      var onReady = this.props.onReady;

      if (onReady) {
        nextTick(function () {
          var nodes = getContexts(_this2.itemRefs, _this2.state.sliderIdPrefix);
          onReady(nodes, _this2.itemRefs);
        });
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this3 = this;

      var _a = this.props,
          items = _a.items,
          activeIndex = _a.activeIndex,
          _a$prefix = _a.prefix,
          prefix = _a$prefix === void 0 ? 'mt-' : _a$prefix,
          autoPlay = _a.autoPlay,
          others = __rest(_a, ["items", "activeIndex", "prefix", "autoPlay"]);

      var sliderHeight = px2rpx(windowHeight);
      var _this$state = this.state,
          visible = _this$state.visible,
          curIndex = _this$state.curIndex;
      var clsPrefix = "".concat(prefix, "slider");
      return /*#__PURE__*/React.createElement(Modal, {
        className: "".concat(clsPrefix, "-modal"),
        containerClassName: "".concat(clsPrefix, "-modal-container"),
        visible: visible,
        onClose: this.handleClose
      }, /*#__PURE__*/React.createElement(View, {
        className: "".concat(clsPrefix, "-control")
      }, /*#__PURE__*/React.createElement(View, {
        className: "".concat(clsPrefix, "-close-wrapper"),
        onClick: this.handleClose
      }, /*#__PURE__*/React.createElement(Icon, {
        name: "close",
        className: "".concat(clsPrefix, "-close")
      })), /*#__PURE__*/React.createElement(Text, {
        className: "".concat(clsPrefix, "-pagination")
      }, curIndex + 1, "/", items.length)), /*#__PURE__*/React.createElement(Slider, _extends({}, others, {
        activeIndex: activeIndex,
        showDots: false,
        width: 750,
        height: sliderHeight,
        onChange: this.handleChange
      }), items.map(function (item, index) {
        var _item = item;

        if (isString(item)) {
          _item = {
            src: item,
            type: 'image'
          };
        }

        var _item2 = _item,
            src = _item2.src,
            type = _item2.type,
            itemHeight = _item2.height,
            rest = __rest(_item, ["src", "type", "height"]);

        var isImage = type === 'image';

        var _height = itemHeight || sliderHeight || '100%';

        var imgStyle = {};

        if (isImage) {
          imgStyle = {
            backgroundImage: "url(".concat(_item.src, ")"),
            height: _height
          };
        }

        return /*#__PURE__*/React.createElement(SliderItem, {
          key: "".concat(index, "-").concat(item)
        }, /*#__PURE__*/React.createElement(View, {
          className: classNames("".concat(prefix, "slider-open-item"), _defineProperty({}, "".concat(prefix, "slider-open-item--image"), isImage)),
          style: __rpx2vw__(Object.assign({}, imgStyle)),
          onClick: function onClick() {
            _this3.handleItemClick(isImage);
          }
        }, type === 'video' ? /*#__PURE__*/React.createElement(Video, _extends({
          ref: function ref(r) {
            _this3.itemRefs[index] = r;
          },
          id: "".concat(_this3.state.sliderIdPrefix, "-").concat(index)
        }, rest, {
          src: _item.src,
          style: __rpx2vw__({
            width: '750rpx'
          }),
          autoPlay: autoPlay,
          showCenterPlayBtn: true,
          "enable-progress-gesture": false
        })) : null));
      })));
    }
  }]);

  return SliderWrapper;
}(Component);

SliderWrapper.defaultProps = {
  items: [],
  activeIndex: 0,
  onClick: function onClick() {},
  onClose: function onClose() {},
  onChange: function onChange() {}
};
export default function (options) {
  if (isStrictWechatMiniProgram) {
    var items = options.items,
        activeIndex = options.activeIndex,
        onSuccess = options.onSuccess,
        onFail = options.onFail,
        onComplete = options.onComplete,
        others = __rest(options, ["items", "activeIndex", "onSuccess", "onFail", "onComplete"]);

    return wx.previewMedia({
      sources: items.map(function (item) {
        return Object.assign(Object.assign({}, item), {
          url: item.src
        });
      }),
      current: activeIndex,
      success: onSuccess,
      fail: onFail,
      complete: onComplete,
      others: others
    });
  } else if (isStrictMiniApp) {
    var _items = options.items,
        _activeIndex = options.activeIndex,
        _onSuccess = options.onSuccess,
        _onFail = options.onFail,
        _onComplete = options.onComplete,
        _others = __rest(options, ["items", "activeIndex", "onSuccess", "onFail", "onComplete"]);

    return my.previewImage(Object.assign({
      urls: _items.filter(function (item) {
        return item.type === 'image';
      }).map(function (item) {
        return item.src;
      }),
      current: _activeIndex,
      success: _onSuccess,
      fail: _onFail,
      complete: _onComplete
    }, _others));
  } else {
    var unMount = renderToRoot( /*#__PURE__*/React.createElement(SliderWrapper, options));
    return {
      close: function close() {
        return unMount();
      }
    };
  }
}