"use strict";

exports.__esModule = true;
exports.default = SelectedListDrawer;

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

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

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

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

var _list = _interopRequireDefault(require("../../list"));

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

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

var _option = _interopRequireDefault(require("../option"));

var _util = require("../util");

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 SelectedListDrawer(props) {
  var prefix = props.prefix,
      innerValue = props.innerValue,
      locale = props.locale;
  var visible = props.visible,
      onOk = props.onOk;
  var clsPrefix = prefix + "select";

  var _useState = (0, _react.useState)([]),
      selectedList = _useState[0],
      setSelectedList = _useState[1];

  var handleItemClick = function (item) {
    var isSelected = (0, _utils.findInArray)(selectedList, function (it) {
      return it.value === item.value;
    });

    if (isSelected) {
      var arr = (0, _util.removeItemFromArray)(selectedList, item.value);
      setSelectedList(arr);
    } else {
      setSelectedList([].concat(selectedList, [item]));
    }
  };

  (0, _react.useEffect)(function () {
    setSelectedList(innerValue);
  }, [innerValue]);
  return /*#__PURE__*/(0, _react.createElement)(_drawer.default, {
    visible: visible,
    closeMode: ['mask'],
    className: clsPrefix + "-confirm-drawer"
  }, /*#__PURE__*/(0, _react.createElement)(_view.default, {
    className: clsPrefix + "-confirm-drawer-header"
  }, /*#__PURE__*/(0, _react.createElement)(_meetReactComponentOne.Text, {
    className: clsPrefix + "-confirm-total"
  }, locale.totalPrefixText, " ", selectedList.length, " ", locale.totalSuffixText), /*#__PURE__*/(0, _react.createElement)(_button.default, {
    type: "primary",
    model: "text",
    onClick: function handleOk() {
      onOk(selectedList);
    }
  }, locale.secondaryOkText)), /*#__PURE__*/(0, _react.createElement)(_list.default, null, innerValue.map(function (item) {
    var selected = (0, _utils.findInArray)(selectedList, function (it) {
      return it.value === item.value;
    });
    return /*#__PURE__*/(0, _react.createElement)(_option.default, {
      selected: selected,
      key: item.value,
      onClick: function onClick() {
        handleItemClick(item);
      }
    }, item.label);
  })));
}

SelectedListDrawer.displayName = 'SelectedListDrawer';