"use strict";

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

var _tslib = require("tslib");

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

var _classnames = _interopRequireDefault(require("classnames"));

var _meetReactComponentOne = require("@alifd/meet-react-component-one");

var _badge = _interopRequireDefault(require("../badge"));

var _drawer = _interopRequireDefault(require("../drawer"));

var _locale = require("../locale");

var _zhCn = _interopRequireDefault(require("../locale/lang/zh-cn"));

var _safeArea = _interopRequireDefault(require("../safe-area"));

var _view = _interopRequireDefault(require("../view"));

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

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 (props, ref) {
  var _classNames2, _classNames3;

  var _props$prefix = props.prefix,
      prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
      message = props.message,
      options = props.options,
      _props$visible = props.visible,
      visible = _props$visible === void 0 ? false : _props$visible,
      _props$fullWidth = props.fullWidth,
      fullWidth = _props$fullWidth === void 0 ? true : _props$fullWidth,
      _props$showCancel = props.showCancel,
      showCancel = _props$showCancel === void 0 ? true : _props$showCancel,
      _props$maskClosable = props.maskClosable,
      maskClosable = _props$maskClosable === void 0 ? true : _props$maskClosable,
      destructiveIndex = props.destructiveIndex,
      cancelText = props.cancelText,
      alignProps = props.align,
      disabledIndexes = props.disabledIndexes,
      onClick = props.onClick,
      onClose = props.onClose,
      className = props.className,
      others = (0, _tslib.__rest)(props, ["prefix", "message", "options", "visible", "fullWidth", "showCancel", "maskClosable", "destructiveIndex", "cancelText", "align", "disabledIndexes", "onClick", "onClose", "className"]);
  var locale = (0, _locale.useLocale)('ActionSheet', others, _zhCn.default.ActionSheet);
  var handleOptionClick = (0, _react.useCallback)(function (option, index) {
    if (onClick) {
      onClick(option, index);
    }

    if (onClose) {
      onClose(index);
    }
  }, [onClick, onClose]);
  var clsPrefix = prefix + "actionsheet";
  var buttons = (0, _react.useMemo)(function () {
    if (!options) {
      return null;
    }

    var len = options.length;
    return options.map(function (option, index) {
      var _classNames;

      var disabled = disabledIndexes ? disabledIndexes.indexOf(index) > -1 : false;
      var content = null;
      var align = alignProps || (typeof option === 'object' && option.icon ? 'left' : 'center');

      if ((0, _utils.isString)(option)) {
        content = /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
          className: clsPrefix + "-option-text " + clsPrefix + "-option--" + align
        }, option);
      } else if (typeof option === 'object') {
        var badge = option.badge,
            icon = option.icon,
            text = option.text,
            badgeType = option.badgeType;
        content = /*#__PURE__*/(0, _react.createElement)(_react.Fragment, null, icon ? /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Image, {
          source: {
            uri: icon
          },
          className: clsPrefix + "-icon"
        }) : null, /*#__PURE__*/(0, _react.createElement)(_view.default, {
          className: clsPrefix + "-option-content " + clsPrefix + "-option--" + align
        }, /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
          className: clsPrefix + "-option-text"
        }, text), badge ? /*#__PURE__*/(0, _react.createElement)(_badge.default, {
          className: clsPrefix + "-badge",
          count: badge,
          dot: badgeType === 'dot'
        }) : null));
      }

      return /*#__PURE__*/(0, _react.createElement)(_view.default, {
        key: clsPrefix + "-option-" + index,
        onClick: function () {
          if (!disabled) {
            handleOptionClick(option, index);
          }
        },
        className: (0, _classnames.default)(clsPrefix + "-option", (_classNames = {}, _classNames[clsPrefix + "-option--destructive"] = destructiveIndex === index, _classNames[clsPrefix + "-option--disabled"] = disabled, _classNames[clsPrefix + "-option--advanced"] = typeof option === 'object' && option.icon, _classNames[clsPrefix + "-option--last"] = index === len - 1, _classNames))
      }, content);
    });
  }, [options, clsPrefix, disabledIndexes, destructiveIndex, alignProps, handleOptionClick]);
  var handleMaskClose = (0, _react.useCallback)(function () {
    return onClose && onClose('mask');
  }, [onClose]);
  var handleCancelClose = (0, _react.useCallback)(function () {
    return onClose && onClose('cancel');
  }, [onClose]);
  return /*#__PURE__*/(0, _react.createElement)(_drawer.default, _extends({}, others, {
    ref: ref,
    visible: visible,
    closeMode: maskClosable ? ['mask'] : [],
    className: (0, _classnames.default)(clsPrefix + "-drawer", className),
    placement: "bottom",
    onClose: handleMaskClose,
    disableScroll: true
  }), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: (0, _classnames.default)("" + clsPrefix, (_classNames2 = {}, _classNames2[clsPrefix + "--fullwidth"] = fullWidth, _classNames2))
  }, message ? /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
    className: clsPrefix + "-msg",
    numberOfLines: 1
  }, message) : null, /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: (0, _classnames.default)(clsPrefix + "-option-list")
  }, buttons), showCancel ? /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: (0, _classnames.default)(clsPrefix + "-option", clsPrefix + "-cancel"),
    onClick: handleCancelClose
  }, /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
    className: clsPrefix + "-option-text"
  }, cancelText || locale.cancel || 'Cancel')) : null, /*#__PURE__*/(0, _react.createElement)(_safeArea.default, {
    className: (0, _classnames.default)(clsPrefix + "-safe-area", (_classNames3 = {}, _classNames3[clsPrefix + "-safe-area--fullwidth"] = fullWidth, _classNames3))
  })));
};

ActionSheet.displayName = 'ActionSheet';

var _default = /*#__PURE__*/(0, _react.forwardRef)(ActionSheet);

exports.default = _default;