function _extends() {
  _extends = Object.assign ? Object.assign.bind() : 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 classnames from "classnames";
import { pick } from "../utils";
import Popup from "../popup";
import ConfigProviderContext from "../config-provider/ConfigProviderContext";
const 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;
}
const ShareSheet = (props) => {
  const {
    prefixCls,
    createNamespace
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace("share-sheet", prefixCls);
  const onCancel = () => {
    var _a;
    (_a = props.onCancel) == null ? void 0 : _a.call(props);
  };
  const onSelect = (option, index) => {
    var _a;
    (_a = props.onSelect) == null ? void 0 : _a.call(props, option, index);
  };
  const renderHeader = () => {
    const {
      title,
      description
    } = props;
    if (title || description) {
      return /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("header"))
      }, title && /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("title"))
      }, title), description && /* @__PURE__ */ React.createElement("div", {
        className: classnames(bem("description"))
      }, description));
    }
    return null;
  };
  const renderOption = (option, index) => {
    const {
      name,
      icon,
      className,
      description
    } = option;
    return /* @__PURE__ */ React.createElement("div", {
      key: index,
      role: "button",
      tabIndex: 0,
      className: classnames([bem("option"), className]),
      onClick: () => onSelect(option, index)
    }, /* @__PURE__ */ React.createElement("img", {
      alt: "share sheet icon",
      src: getIconURL(icon),
      className: classnames(bem("icon"))
    }), name && /* @__PURE__ */ React.createElement("span", {
      className: classnames(bem("name"))
    }, name), description && /* @__PURE__ */ React.createElement("span", {
      className: classnames(bem("option-description"))
    }, description));
  };
  const renderOptions = (options, border, key) => /* @__PURE__ */ React.createElement("div", {
    key,
    className: classnames(bem("options", {
      border
    }))
  }, options.map(renderOption));
  const renderRows = () => {
    const {
      options
    } = props;
    if (Array.isArray(options[0])) {
      return options.map((item, index) => renderOptions(item, index !== 0, index));
    }
    return renderOptions(options);
  };
  const renderCancelButton = () => {
    const {
      cancelText
    } = props;
    if (cancelText) {
      return /* @__PURE__ */ React.createElement("button", {
        type: "button",
        className: classnames(bem("cancel")),
        onClick: onCancel
      }, cancelText);
    }
    return null;
  };
  return /* @__PURE__ */ React.createElement(Popup, _extends({
    round: true,
    className: classnames(bem()),
    position: "bottom",
    onClose: onCancel
  }, pick(props, ["closeOnPopstate", "safeAreaInsetBottom", "visible", "overlay", "duration", "lockScroll", "overlayStyle", "overlayClass", "closeOnClickOverlay"])), renderHeader(), renderRows(), renderCancelButton());
};
ShareSheet.defaultProps = {
  cancelText: "\u53D6\u6D88",
  options: [],
  closeOnPopstate: true,
  safeAreaInsetBottom: true,
  closeOnClickOverlay: true
};
var stdin_default = ShareSheet;
export {
  stdin_default as default
};
