"use strict";

exports.__esModule = true;
exports["default"] = void 0;

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

var _reactDom = require("react-dom");

var _instance = _interopRequireDefault(require("./../Context/instance.js"));

var _excluded = ["portal", "show", "animation", "duration", "list", "maskAttribute", "groupAttribute", "optionAttribute", "cancelCaption", "cancelAttribute", "onChange"],
    _excluded2 = ["onClick"];

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

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var Actionsheet = (0, _react.forwardRef)(function (_ref, ref) {
  var portal = _ref.portal,
      _ref$show = _ref.show,
      show = _ref$show === void 0 ? false : _ref$show,
      _ref$animation = _ref.animation,
      animation = _ref$animation === void 0 ? 'slideUp' : _ref$animation,
      duration = _ref.duration,
      list = _ref.list,
      _ref$maskAttribute = _ref.maskAttribute,
      maskAttribute = _ref$maskAttribute === void 0 ? {} : _ref$maskAttribute,
      _ref$groupAttribute = _ref.groupAttribute,
      groupAttribute = _ref$groupAttribute === void 0 ? {} : _ref$groupAttribute,
      _ref$optionAttribute = _ref.optionAttribute,
      optionAttribute = _ref$optionAttribute === void 0 ? {} : _ref$optionAttribute,
      _ref$cancelCaption = _ref.cancelCaption,
      cancelCaption = _ref$cancelCaption === void 0 ? '' : _ref$cancelCaption,
      _ref$cancelAttribute = _ref.cancelAttribute,
      cancelAttribute = _ref$cancelAttribute === void 0 ? {} : _ref$cancelAttribute,
      onChange = _ref.onChange,
      others = _objectWithoutPropertiesLoose(_ref, _excluded);

  // context
  var context = (0, _react.useContext)(_instance["default"]) || {};

  var locale = context.locale || function (remark) {
    return remark || '';
  };

  function handleClick(e) {
    var target = e.target; // 获取值

    var item = null;
    var index = null;

    if (target.classList.contains('actionsheet-option')) {
      index = target.getAttribute('data-index');
      index = Number(index);
      item = list[index] || null;
    }

    if (item && item.onClick) {
      list[index].onClick(e, item.name, [item]);
      return;
    } // 区分点击事件


    e.stopPropagation();

    if (target.classList.contains('actionsheet-option')) {
      if (optionAttribute.onClick) {
        optionAttribute.onClick(e, item.name, [item]);
        return;
      }
    }

    if (target.classList.contains('actionsheet-group') && groupAttribute.onClick) {
      groupAttribute.onClick(e);
      return;
    }

    if (target.classList.contains('actionsheet-mask') && maskAttribute.onClick) {
      maskAttribute.onClick(e);
      return;
    }

    if (target.classList.contains('actionsheet-cancel') && cancelAttribute.onClick) {
      cancelAttribute.onClick(e);
      return;
    }

    if (target.classList.contains('actionsheet-option')) {
      if (onChange) onChange(e, item.name, [item], index);
    }
  } // 构建动画


  var animationClassName = '';

  switch (animation) {
    case 'slideLeft':
      animationClassName = 'popup-animation right-middle';
      break;

    case 'slideRight':
      animationClassName = 'popup-animation left-middle';
      break;

    case 'slideUp':
      animationClassName = 'popup-animation bottom-center';
      break;

    case 'slideDown':
      animationClassName = 'popup-animation top-center';
      break;

    case 'zoom':
      animationClassName = 'popup-animation middle';
      break;

    case 'fade':
      animationClassName = 'popup-animation middle';
      break;

    case 'none':
      animationClassName = '';
      break;

    default:
      animationClassName = 'popup-animation middle';
  } // 动画时长


  var durationStyle = {};

  if (typeof duration === 'number') {
    durationStyle = {
      WebkitTransitionDuration: duration + 'ms'
    };
  } // 过滤已经回调的属性


  function filterProps(props) {
    if (!props) return {};

    var onClick = props.onClick,
        otherProps = _objectWithoutPropertiesLoose(props, _excluded2);

    return _extends({}, otherProps);
  } // 剔除掉onClick事件, 因为在容器onClick已经回调了


  var otherMaskAttribute = filterProps(maskAttribute);
  var otherGroupAttribute = filterProps(groupAttribute);
  var otherOptionAttribute = filterProps(optionAttribute);
  var otherCancelAttribute = filterProps(cancelAttribute);
  return (0, _reactDom.createPortal)( /*#__PURE__*/_react["default"].createElement("div", _extends({
    ref: ref
  }, otherMaskAttribute, {
    className: "mask actionsheet-mask" + (otherMaskAttribute.className ? ' ' + otherMaskAttribute.className : '') + (show ? ' active' : ''),
    style: Object.assign({}, durationStyle, otherMaskAttribute.style || {}),
    onClick: handleClick
  }), /*#__PURE__*/_react["default"].createElement("div", _extends({
    "data-animation": animation
  }, others, {
    className: "actionsheet" + (animationClassName ? ' ' + animationClassName : '') + (others.className ? ' ' + others.className : '') + (show ? ' active' : ''),
    style: Object.assign({}, durationStyle, others.style || {})
  }), /*#__PURE__*/_react["default"].createElement("div", _extends({}, otherGroupAttribute, {
    className: "actionsheet-group" + (otherGroupAttribute.className ? ' ' + otherGroupAttribute.className : '')
  }), list && list.map(function (item, index) {
    return /*#__PURE__*/_react["default"].createElement("div", _extends({}, otherOptionAttribute, {
      className: "actionsheet-option" + (otherOptionAttribute.className ? ' ' + otherOptionAttribute.className : ' border-b'),
      key: index,
      "data-index": index
    }), item.name);
  })), cancelAttribute.onClick && /*#__PURE__*/_react["default"].createElement("div", _extends({}, otherCancelAttribute, {
    className: "actionsheet-cancel" + (otherCancelAttribute.className ? ' ' + otherCancelAttribute.className : '')
  }), cancelCaption || locale('取消', 'cancel')))), portal || context.portal || document.getElementById('root') || document.body);
});
var _default = Actionsheet;
exports["default"] = _default;
module.exports = exports.default;