"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

function _jsxRuntime() {
  const data = require("react/jsx-runtime");

  _jsxRuntime = function () {
    return data;
  };

  return data;
}

function _react() {
  const data = _interopRequireWildcard(require("react"));

  _react = function () {
    return data;
  };

  return data;
}

function _icons() {
  const data = require("@react-vant/icons");

  _icons = function () {
    return data;
  };

  return data;
}

function _clsx() {
  const data = _interopRequireDefault(require("clsx"));

  _clsx = function () {
    return data;
  };

  return data;
}

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

const ActionSheet = props => {
  const {
    prefixCls,
    createNamespace
  } = (0, _react().useContext)(_ConfigProviderContext.default);
  const [bem] = createNamespace('action-sheet', prefixCls);

  const onCancel = () => {
    var _a, _b;

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

  const renderHeader = () => {
    if (!props.title) return null;
    return (0, _jsxRuntime().jsxs)("div", Object.assign({
      className: (0, _clsx().default)(bem('header'))
    }, {
      children: [props.title, props.closeable && _react().default.cloneElement(props.closeIcon, {
        className: (0, _clsx().default)(bem('clear')),
        onClick: onCancel
      })]
    }));
  };

  const renderCancel = () => {
    if (!props.cancelText) return null;
    return [(0, _jsxRuntime().jsx)("div", {
      className: (0, _clsx().default)(bem('gap'))
    }, "cancel-gap"), (0, _jsxRuntime().jsx)("button", Object.assign({
      type: "button",
      className: (0, _clsx().default)(bem('cancel')),
      onClick: onCancel
    }, {
      children: props.cancelText
    }), "cancel-btn")];
  };

  const renderOption = (item, index) => {
    const {
      name,
      color,
      subname,
      loading,
      callback,
      disabled,
      className,
      style
    } = item;
    const Content = loading ? (0, _jsxRuntime().jsx)(_loading.default, {
      className: (0, _clsx().default)(bem('loading-icon'))
    }) : [(0, _jsxRuntime().jsx)("span", Object.assign({
      className: (0, _clsx().default)(bem('name'))
    }, {
      children: name
    }), `${index}-1`), subname && (0, _jsxRuntime().jsx)("div", Object.assign({
      className: (0, _clsx().default)(bem('subname'))
    }, {
      children: subname
    }), `${index}-2`)];

    const onClick = () => {
      if (disabled || loading) {
        return;
      }

      if (callback) {
        callback(item);
      }

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

      setTimeout(() => {
        var _a;

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

    return (0, _jsxRuntime().jsx)("button", Object.assign({
      type: "button",
      style: Object.assign({
        color
      }, style),
      className: (0, _clsx().default)(bem('item', {
        loading,
        disabled
      }), className),
      onClick: onClick
    }, {
      children: Content
    }), index);
  };

  const renderDescription = () => {
    if (props.description) {
      return (0, _jsxRuntime().jsx)("div", Object.assign({
        className: (0, _clsx().default)(bem('description'))
      }, {
        children: props.description
      }));
    }

    return null;
  };

  const renderOptions = () => {
    if (props.actions) {
      return props.actions.map(renderOption);
    }

    return null;
  };

  return (0, _jsxRuntime().jsx)(_popup.default, Object.assign({
    visible: props.visible,
    className: (0, _clsx().default)(bem('wrapperr')),
    position: "bottom"
  }, (0, _utils.pick)(props, _Popup.sharedPopupProps), {
    onClose: onCancel,
    closeable: false
  }, {
    children: (0, _jsxRuntime().jsxs)("div", Object.assign({
      className: (0, _clsx().default)(bem(), props.className),
      style: props.style
    }, {
      children: [renderHeader(), renderDescription(), (0, _jsxRuntime().jsxs)("div", Object.assign({
        className: (0, _clsx().default)(bem('content'))
      }, {
        children: [renderOptions(), props.children]
      })), renderCancel()]
    }))
  }));
};

ActionSheet.defaultProps = {
  closeable: true,
  safeAreaInsetBottom: true,
  round: true,
  lockScroll: true,
  overlay: true,
  closeOnClickOverlay: true,
  closeIcon: (0, _jsxRuntime().jsx)(_icons().Cross, {})
};
var _default = ActionSheet;
exports.default = _default;