import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useContext } from 'react';
import cls from 'clsx';
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,
    locale
  } = useContext(ConfigProviderContext);
  const [bem] = createNamespace('share-sheet', prefixCls);

  const onCancel = () => {
    var _a;

    (_a = props.onCancel) === null || _a === void 0 ? void 0 : _a.call(props);
  };

  const onSelect = (option, index) => {
    var _a;

    (_a = props.onSelect) === null || _a === void 0 ? void 0 : _a.call(props, option, index);
  };

  const renderHeader = () => {
    const {
      title,
      description
    } = props;

    if (title || description) {
      return _jsxs("div", Object.assign({
        className: cls(bem('header'))
      }, {
        children: [title && _jsx("div", Object.assign({
          className: cls(bem('title'))
        }, {
          children: title
        })), description && _jsx("div", Object.assign({
          className: cls(bem('description'))
        }, {
          children: description
        }))]
      }));
    }

    return null;
  };

  const renderOption = (option, index) => {
    const {
      name,
      icon,
      className,
      description
    } = option;
    return _jsxs("div", Object.assign({
      role: "button",
      tabIndex: 0,
      className: cls([bem('option'), className]),
      onClick: () => onSelect(option, index)
    }, {
      children: [typeof icon === 'string' ? _jsx("img", {
        alt: "share sheet icon",
        src: getIconURL(icon),
        className: cls(bem('icon'))
      }) : icon, name && _jsx("span", Object.assign({
        className: cls(bem('name'))
      }, {
        children: name
      })), description && _jsx("span", Object.assign({
        className: cls(bem('option-description'))
      }, {
        children: description
      }))]
    }), index);
  };

  const renderOptions = (options, border, key) => _jsx("div", Object.assign({
    className: cls(bem('options', {
      border
    }))
  }, {
    children: options.map(renderOption)
  }), key);

  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 = locale.cancel
    } = props;

    if (cancelText) {
      return _jsx("button", Object.assign({
        type: "button",
        className: cls(bem('cancel')),
        onClick: onCancel
      }, {
        children: cancelText
      }));
    }

    return null;
  };

  return _jsxs(Popup, Object.assign({
    round: true,
    className: cls(bem()),
    position: "bottom",
    onClose: onCancel
  }, pick(props, ['closeOnPopstate', 'safeAreaInsetBottom', 'visible', 'overlay', 'duration', 'lockScroll', 'overlayStyle', 'overlayClass', 'closeOnClickOverlay']), {
    children: [renderHeader(), renderRows(), renderCancelButton()]
  }));
};

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