"use strict";

exports.__esModule = true;
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _clsx = _interopRequireDefault(require("clsx"));

var _utils = require("../utils");

var _popup = _interopRequireDefault(require("../popup"));

var _ConfigProviderContext = _interopRequireDefault(require("../config-provider/ConfigProviderContext"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

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 = (0, _react.useContext)(_ConfigProviderContext.default),
      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.default.createElement("div", {
        className: (0, _clsx.default)(bem('header'))
      }, title && /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('title'))
      }, title), description && /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(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.default.createElement("div", {
      key: index,
      role: "button",
      tabIndex: 0,
      className: (0, _clsx.default)([bem('option'), className]),
      onClick: function onClick() {
        return onSelect(option, index);
      }
    }, typeof icon === 'string' ? /*#__PURE__*/_react.default.createElement("img", {
      alt: "share sheet icon",
      src: getIconURL(icon),
      className: (0, _clsx.default)(bem('icon'))
    }) : icon, name && /*#__PURE__*/_react.default.createElement("span", {
      className: (0, _clsx.default)(bem('name'))
    }, name), description && /*#__PURE__*/_react.default.createElement("span", {
      className: (0, _clsx.default)(bem('option-description'))
    }, description));
  };

  var renderOptions = function renderOptions(options, border, key) {
    return /*#__PURE__*/_react.default.createElement("div", {
      key: key,
      className: (0, _clsx.default)(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.default.createElement("button", {
        type: "button",
        className: (0, _clsx.default)(bem('cancel')),
        onClick: onCancel
      }, cancelText);
    }

    return null;
  };

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

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