"use strict";

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

var _babelRuntimeJsxStyleTransform = require("babel-runtime-jsx-style-transform");

var _tslib = require("tslib");

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

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

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

var _button = _interopRequireDefault(require("../button"));

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

var _form = require("../form");

var _icon = _interopRequireDefault(require("../icon"));

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

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

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

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

var _children2 = require("../utils/children");

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

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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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 SelectDrawer = function (props, ref) {
  var _classNames3, _classNames4;

  var _props$prefix = props.prefix,
      prefix = _props$prefix === void 0 ? 'mt-' : _props$prefix,
      alignProp = props.align,
      children = props.children,
      _props$drawerTitle = props.drawerTitle,
      drawerTitle = _props$drawerTitle === void 0 ? '' : _props$drawerTitle,
      _props$iconType = props.iconType,
      iconType = _props$iconType === void 0 ? 'arrow-right' : _props$iconType,
      _props$content = props.content,
      content = _props$content === void 0 ? '' : _props$content,
      placeholder = props.placeholder,
      _props$type = props.type,
      type = _props$type === void 0 ? 'normal' : _props$type,
      className = props.className,
      drawerClassName = props.drawerClassName,
      _props$disabled = props.disabled,
      disabled = _props$disabled === void 0 ? false : _props$disabled,
      _props$readOnly = props.readOnly,
      readOnly = _props$readOnly === void 0 ? false : _props$readOnly,
      _props$showIcon = props.showIcon,
      showIcon = _props$showIcon === void 0 ? true : _props$showIcon,
      _props$hasClear = props.hasClear,
      hasClear = _props$hasClear === void 0 ? false : _props$hasClear,
      _props$showToolbar = props.showToolbar,
      showToolbar = _props$showToolbar === void 0 ? true : _props$showToolbar,
      notFoundContent = props.notFoundContent,
      onOk = props.onOk,
      onCancel = props.onCancel,
      onClear = props.onClear,
      onVisibleChange = props.onVisibleChange,
      okText = props.okText,
      cancelText = props.cancelText,
      zIndex = props.zIndex,
      sizeProp = props.size,
      disableScroll = props.disableScroll,
      others = (0, _tslib.__rest)(props, ["prefix", "align", "children", "drawerTitle", "iconType", "content", "placeholder", "type", "className", "drawerClassName", "disabled", "readOnly", "showIcon", "hasClear", "showToolbar", "notFoundContent", "onOk", "onCancel", "onClear", "onVisibleChange", "okText", "cancelText", "zIndex", "size", "disableScroll"]);

  var _useState = (0, _react.useState)(false),
      visible = _useState[0],
      setVisible = _useState[1];

  var _useContext = (0, _react.useContext)(_form.ItemContext),
      contextAlign = _useContext.contentAlign,
      contextSize = _useContext.size,
      isInsideForm = _useContext.isInsideForm;

  var _children = (0, _children2.getValidChildren)(children);

  var align = alignProp || (isInsideForm ? contextAlign : null) || 'left';
  var size = sizeProp || (isInsideForm ? contextSize : null) || 'medium';
  var shouldShowClear = !disabled && hasClear && content;
  var isInverse = type === 'inverse';
  var locale = (0, _locale.useLocale)('SelectDrawer', others, _zhCn.default.SelectDrawer);
  var clsPrefix = prefix + "selectdrawer";
  var doHide = (0, _react.useCallback)(function () {
    if (visible) {
      setVisible(false);
    }

    if (onVisibleChange) {
      onVisibleChange(false);
    }
  }, [visible, setVisible, onVisibleChange]);
  var handleOk = (0, _react.useCallback)(function () {
    var shouldHide = true;

    if (onOk) {
      shouldHide = onOk();
    }

    if (shouldHide !== false) {
      doHide();
    }
  }, [onOk, doHide]);
  var handleCancel = (0, _react.useCallback)(function (reason) {
    var shouldHide = true;

    if (onCancel) {
      shouldHide = onCancel(reason);
    }

    if (shouldHide !== false) {
      doHide();
    }
  }, [onCancel, doHide]);
  var handleClear = (0, _react.useCallback)(function (e) {
    e.preventDefault();
    (0, _utils.stopPropagation)(e);

    if (onClear) {
      onClear();
    }
  }, [onClear]);

  var handleOpen = function () {
    if (disabled || readOnly || visible) {
      return;
    }

    if (_children || notFoundContent) {
      setVisible(true);

      if (onVisibleChange) {
        onVisibleChange(true);
      }
    }
  };

  (0, _react.useImperativeHandle)(ref, function () {
    return Object.assign(ref.current || {}, {
      hide: doHide,
      show: handleOpen
    });
  });
  return /*#__PURE__*/(0, _react.createElement)(_react.Fragment, null, /*#__PURE__*/(0, _react.createElement)(_view.default, _extends({
    ref: ref
  }, others, {
    className: (0, _classnames.default)(clsPrefix, clsPrefix + "--" + size, className),
    onClick: handleOpen
  }), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: (0, _classnames.default)(clsPrefix + "-content", clsPrefix + "-content--" + size, clsPrefix + "-content--" + align)
  }, function getContent() {
    if (!content) {
      var _classNames;

      return /*#__PURE__*/(0, _react.createElement)(_view.default, {
        className: (0, _classnames.default)(clsPrefix + "-placeholder", clsPrefix + "-placeholder--" + align, (_classNames = {}, _classNames[clsPrefix + "-placeholder--disabled"] = disabled, _classNames[clsPrefix + "-placeholder--inverse"] = isInverse, _classNames[clsPrefix + "-placeholder--inverse-disabled"] = disabled && isInverse, _classNames))
      }, placeholder || locale.placeHolderText);
    } else if ((0, _utils.isString)(content)) {
      var _classNames2;

      return /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
        numberOfLines: 1,
        className: (0, _classnames.default)(clsPrefix + "-content-text", clsPrefix + "-content-text--" + align, (_classNames2 = {}, _classNames2[clsPrefix + "-content-text--disabled"] = disabled, _classNames2[clsPrefix + "-content-text--inverse"] = isInverse, _classNames2[clsPrefix + "-content-text--inverse-disabled"] = isInverse && disabled, _classNames2)),
        style: (0, _babelRuntimeJsxStyleTransform.rpx2vw4style)({
          textAlign: align
        })
      }, content);
    } else {
      return content;
    }
  }()), showIcon && !shouldShowClear ? /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: (0, _classnames.default)(clsPrefix + "-icon", clsPrefix + "-icon--" + size)
  }, /*#__PURE__*/(0, _react.createElement)(_icon.default, {
    className: (0, _classnames.default)(clsPrefix + "-icon-text", clsPrefix + "-icon-text--" + size, (_classNames3 = {}, _classNames3[clsPrefix + "-icon-text--inverse"] = isInverse, _classNames3[clsPrefix + "-icon-text--disabled"] = disabled, _classNames3[clsPrefix + "-icon-text--inverse-disabled"] = isInverse && disabled, _classNames3)),
    name: iconType
  })) : null, shouldShowClear ? /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: (0, _classnames.default)(clsPrefix + "-icon", clsPrefix + "-icon--" + size),
    onClick: handleClear
  }, /*#__PURE__*/(0, _react.createElement)(_icon.default, {
    className: (0, _classnames.default)(clsPrefix + "-icon-text--" + size, (_classNames4 = {}, _classNames4[clsPrefix + "-icon-text--inverse"] = isInverse, _classNames4[clsPrefix + "-icon-text--disabled"] = disabled, _classNames4[clsPrefix + "-icon-text--inverse-disabled"] = isInverse && disabled, _classNames4)),
    name: "delete-filling"
  })) : null), /*#__PURE__*/(0, _react.createElement)(_drawer.default, {
    visible: visible,
    placement: "bottom",
    closeMode: ['mask'],
    onClose: handleCancel,
    zIndex: zIndex,
    className: (0, _classnames.default)(clsPrefix + "-drawer", drawerClassName),
    disableScroll: disableScroll
  }, showToolbar ? /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clsPrefix + "-toolbar"
  }, /*#__PURE__*/(0, _react.createElement)(_button.default, {
    model: "text",
    onClick: function onClick() {
      return handleCancel('cancel-button');
    }
  }, cancelText || locale.cancel), /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clsPrefix + "-toolbar-title"
  }, drawerTitle), /*#__PURE__*/(0, _react.createElement)(_button.default, {
    model: "text",
    type: "primary",
    onClick: handleOk
  }, okText || locale.ok)) : null, function renderChildren() {
    var ret = null;

    if (_children) {
      ret = /*#__PURE__*/(0, _react.createElement)(_view.default, {
        className: clsPrefix + "-drawer-content"
      }, _children);
    } else if ((0, _utils.isString)(notFoundContent)) {
      ret = /*#__PURE__*/(0, _react.createElement)(_view.default, {
        className: clsPrefix + "-drawer-content " + clsPrefix + "-drawer--empty"
      }, /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
        className: clsPrefix + "-drawer-empty-text"
      }, notFoundContent));
    } else if (notFoundContent) {
      ret = /*#__PURE__*/(0, _react.createElement)(_view.default, {
        className: clsPrefix + "-drawer-content " + clsPrefix + "-drawer--empty"
      }, /*#__PURE__*/(0, _react.createElement)(_view.default, {
        className: clsPrefix + "-drawer-empty-text"
      }, notFoundContent));
    }

    return ret;
  }(), /*#__PURE__*/(0, _react.createElement)(_safeArea.default, null)));
};

SelectDrawer.displayName = 'SelectDrawer';

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

exports.default = _default;