"use strict";

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

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

var _icons = require("@react-vant/icons");

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

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

var _loading = _interopRequireDefault(require("../loading"));

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

var _Popup = require("../popup/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 ActionSheet = function ActionSheet(props) {
  var _useContext = (0, _react.useContext)(_ConfigProviderContext.default),
      prefixCls = _useContext.prefixCls,
      createNamespace = _useContext.createNamespace;

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

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

  var renderHeader = function renderHeader() {
    if (!props.title) return null;
    return /*#__PURE__*/_react.default.createElement("div", {
      className: (0, _clsx.default)(bem('header'))
    }, props.title, props.closeable && /*#__PURE__*/_react.default.cloneElement(props.closeIcon, {
      className: (0, _clsx.default)(bem('clear')),
      onClick: onCancel
    }));
  };

  var renderCancel = function renderCancel() {
    if (!props.cancelText) return null;
    return [/*#__PURE__*/_react.default.createElement("div", {
      key: "cancel-gap",
      className: (0, _clsx.default)(bem('gap'))
    }), /*#__PURE__*/_react.default.createElement("button", {
      key: "cancel-btn",
      type: "button",
      className: (0, _clsx.default)(bem('cancel')),
      onClick: onCancel
    }, props.cancelText)];
  };

  var renderOption = function renderOption(item, index) {
    var name = item.name,
        color = item.color,
        subname = item.subname,
        loading = item.loading,
        callback = item.callback,
        disabled = item.disabled,
        className = item.className,
        style = item.style;
    var Content = loading ? /*#__PURE__*/_react.default.createElement(_loading.default, {
      className: (0, _clsx.default)(bem('loading-icon'))
    }) : [/*#__PURE__*/_react.default.createElement("span", {
      key: index + "-1",
      className: (0, _clsx.default)(bem('name'))
    }, name), subname && /*#__PURE__*/_react.default.createElement("div", {
      key: index + "-2",
      className: (0, _clsx.default)(bem('subname'))
    }, subname)];

    var onClick = function onClick() {
      if (disabled || loading) {
        return;
      }

      if (callback) {
        callback(item);
      }

      if (props.closeOnClickAction) {
        onCancel();
      }

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

    return /*#__PURE__*/_react.default.createElement("button", {
      key: index,
      type: "button",
      style: _extends({
        color: color
      }, style),
      className: (0, _clsx.default)(bem('item', {
        loading: loading,
        disabled: disabled
      }), className),
      onClick: onClick
    }, Content);
  };

  var renderDescription = function renderDescription() {
    if (props.description) {
      return /*#__PURE__*/_react.default.createElement("div", {
        className: (0, _clsx.default)(bem('description'))
      }, props.description);
    }

    return null;
  };

  var renderOptions = function renderOptions() {
    if (props.actions) {
      return props.actions.map(renderOption);
    }

    return null;
  };

  return /*#__PURE__*/_react.default.createElement(_popup.default, _extends({
    visible: props.visible,
    className: (0, _clsx.default)(bem()),
    position: "bottom"
  }, (0, _utils.pick)(props, _Popup.sharedPopupProps), {
    onClose: onCancel,
    closeable: false
  }), renderHeader(), renderDescription(), /*#__PURE__*/_react.default.createElement("div", {
    className: (0, _clsx.default)(bem('content'))
  }, renderOptions(), props.children), renderCancel());
};

ActionSheet.defaultProps = {
  closeable: true,
  safeAreaInsetBottom: true,
  round: true,
  lockScroll: true,
  overlay: true,
  closeOnClickOverlay: true,
  closeIcon: /*#__PURE__*/_react.default.createElement(_icons.Cross, null)
};
var _default = ActionSheet;
exports.default = _default;