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

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

import { __rest } from "tslib";
import React, { createElement, forwardRef, Fragment, useCallback, useMemo } from "react";
import classNames from 'classnames';
import { Image, Text } from "@alifd/meet-react-component-one";
import Badge from '../badge';
import Drawer from '../drawer';
import { useLocale } from '../locale';
import defaultLang from '../locale/lang/zh-cn';
import SafeArea from '../safe-area';
import View from '../view';
import { isString } from '../utils';

var ActionSheet = function ActionSheet(props, ref) {
  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 = __rest(props, ["prefix", "message", "options", "visible", "fullWidth", "showCancel", "maskClosable", "destructiveIndex", "cancelText", "align", "disabledIndexes", "onClick", "onClose", "className"]);

  var locale = useLocale('ActionSheet', others, defaultLang.ActionSheet);
  var handleOptionClick = useCallback(function (option, index) {
    if (onClick) {
      onClick(option, index);
    }

    if (onClose) {
      onClose(index);
    }
  }, [onClick, onClose]);
  var clsPrefix = "".concat(prefix, "actionsheet");
  var buttons = 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 (isString(option)) {
        content = /*#__PURE__*/React.createElement(Text, {
          className: "".concat(clsPrefix, "-option-text ").concat(clsPrefix, "-option--").concat(align)
        }, option);
      } else if (_typeof(option) === 'object') {
        var badge = option.badge,
            icon = option.icon,
            text = option.text,
            badgeType = option.badgeType;
        content = /*#__PURE__*/React.createElement(Fragment, null, icon ? /*#__PURE__*/React.createElement(Image, {
          source: {
            uri: icon
          },
          className: "".concat(clsPrefix, "-icon")
        }) : null, /*#__PURE__*/React.createElement(View, {
          className: "".concat(clsPrefix, "-option-content ").concat(clsPrefix, "-option--").concat(align)
        }, /*#__PURE__*/React.createElement(Text, {
          className: "".concat(clsPrefix, "-option-text")
        }, text), badge ? /*#__PURE__*/React.createElement(Badge, {
          className: "".concat(clsPrefix, "-badge"),
          count: badge,
          dot: badgeType === 'dot'
        }) : null));
      }

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

ActionSheet.displayName = 'ActionSheet';
export default /*#__PURE__*/forwardRef(ActionSheet);