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

import React, { useContext } from 'react';
import cls from 'clsx';
import { pick } from '../utils';
import Popup from '../popup';
import ConfigProviderContext from '../config-provider/ConfigProviderContext';
var PRESET_ICONS = ['qq', 'link', 'weibo', 'wechat', 'poster', 'qrcode', 'weapp-qrcode', 'wechat-moments'];

function getIconURL(icon) {
  if (PRESET_ICONS.includes(icon)) {
    return "https://img.yzcdn.cn/vant/share-sheet-" + icon + ".png";
  }

  return icon;
}

var ShareSheet = function ShareSheet(props) {
  var _useContext = useContext(ConfigProviderContext),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace,
      locale = _useContext.locale;

  var _createNamespace = createNamespace('share-sheet', prefixCls),
      bem = _createNamespace[0];

  var onCancel = function onCancel() {
    props.onCancel == null ? void 0 : props.onCancel();
  };

  var onSelect = function onSelect(option, index) {
    props.onSelect == null ? void 0 : props.onSelect(option, index);
  };

  var renderHeader = function renderHeader() {
    var title = props.title,
        description = props.description;

    if (title || description) {
      return /*#__PURE__*/React.createElement("div", {
        className: cls(bem('header'))
      }, title && /*#__PURE__*/React.createElement("div", {
        className: cls(bem('title'))
      }, title), description && /*#__PURE__*/React.createElement("div", {
        className: cls(bem('description'))
      }, description));
    }

    return null;
  };

  var renderOption = function renderOption(option, index) {
    var name = option.name,
        icon = option.icon,
        className = option.className,
        description = option.description;
    return /*#__PURE__*/React.createElement("div", {
      key: index,
      role: "button",
      tabIndex: 0,
      className: cls([bem('option'), className]),
      onClick: function onClick() {
        return onSelect(option, index);
      }
    }, typeof icon === 'string' ? /*#__PURE__*/React.createElement("img", {
      alt: "share sheet icon",
      src: getIconURL(icon),
      className: cls(bem('icon'))
    }) : icon, name && /*#__PURE__*/React.createElement("span", {
      className: cls(bem('name'))
    }, name), description && /*#__PURE__*/React.createElement("span", {
      className: cls(bem('option-description'))
    }, description));
  };

  var renderOptions = function renderOptions(options, border, key) {
    return /*#__PURE__*/React.createElement("div", {
      key: key,
      className: cls(bem('options', {
        border: border
      }))
    }, options.map(renderOption));
  };

  var renderRows = function renderRows() {
    var options = props.options;

    if (Array.isArray(options[0])) {
      return options.map(function (item, index) {
        return renderOptions(item, index !== 0, index);
      });
    }

    return renderOptions(options);
  };

  var renderCancelButton = function renderCancelButton() {
    var _props$cancelText = props.cancelText,
        cancelText = _props$cancelText === void 0 ? locale.cancel : _props$cancelText;

    if (cancelText) {
      return /*#__PURE__*/React.createElement("button", {
        type: "button",
        className: cls(bem('cancel')),
        onClick: onCancel
      }, cancelText);
    }

    return null;
  };

  return /*#__PURE__*/React.createElement(Popup, _extends({
    round: true,
    className: cls(bem()),
    position: "bottom",
    onClose: onCancel
  }, pick(props, ['closeOnPopstate', 'safeAreaInsetBottom', 'visible', 'overlay', 'duration', 'lockScroll', 'overlayStyle', 'overlayClass', 'closeOnClickOverlay'])), renderHeader(), renderRows(), renderCancelButton());
};

ShareSheet.defaultProps = {
  options: [],
  closeOnPopstate: true,
  safeAreaInsetBottom: true,
  closeOnClickOverlay: true
};
export default ShareSheet;