function _extends() { _extends = Object.assign ? Object.assign.bind() : 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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import React, { useState, useMemo } from "react";
import classNames from "classnames";
import { Input, Skeleton } from "antd";
import SearchLg from "@pisell/icon/es/SearchLg";
import AddButton from "../booking/components/addButton";
import Drawer from "../booking/components/drawer";
import TabProduct from "../tabProduct";
import { formatProductListByCategory } from "../booking/utils";
import { changeArray } from "@pisell/utils";
import "./index.less";
var MAX_LENGTH = 999;
var SelectModal = function SelectModal(props) {
  var value = props.value,
    onChange = props.onChange,
    _props$dataSource = props.dataSource,
    dataSource = _props$dataSource === void 0 ? [] : _props$dataSource,
    renderValueItem = props.renderValueItem,
    renderOptionItem = props.renderOptionItem,
    disabled = props.disabled,
    className = props.className,
    style = props.style,
    _props$maxLength = props.maxLength,
    maxLength = _props$maxLength === void 0 ? MAX_LENGTH : _props$maxLength,
    buttonProps = props.buttonProps,
    drawerProps = props.drawerProps,
    tabProductProps = props.tabProductProps,
    loading = props.loading;
  var _useState = useState(false),
    _useState2 = _slicedToArray(_useState, 2),
    open = _useState2[0],
    setOpen = _useState2[1];
  var _useState3 = useState(""),
    _useState4 = _slicedToArray(_useState3, 2),
    search = _useState4[0],
    setSearch = _useState4[1];
  var _value = useMemo(function () {
    return value || [];
  }, [value]);
  var onOpen = function onOpen() {
    setOpen(true);
  };
  var onClose = function onClose() {
    setSearch("");
    setOpen(false);
  };
  var renderList = useMemo(function () {
    var _list = dataSource || [];
    var isIncludes = function isIncludes(str) {
      return str.toLowerCase().indexOf(search.toLowerCase()) !== -1;
    };
    return formatProductListByCategory(_list, search ? function (item) {
      return isIncludes(item.title);
    } : undefined);
  }, [search, dataSource]);
  var onSelect = function onSelect(item) {
    onChange === null || onChange === void 0 || onChange(changeArray(_value, item, "id"));
    onClose();
  };
  return /*#__PURE__*/React.createElement("div", {
    className: classNames("pisell2-select-modal", className),
    style: style
  }, /*#__PURE__*/React.createElement(Skeleton, {
    active: true,
    round: true,
    loading: loading
  }, _value.map(function (item) {
    return renderValueItem === null || renderValueItem === void 0 ? void 0 : renderValueItem(item, function (item) {
      onOpen();
    });
  })), _value.length < maxLength ? /*#__PURE__*/React.createElement(AddButton, _extends({
    onClick: onOpen
  }, buttonProps)) : null, /*#__PURE__*/React.createElement(Drawer, _extends({
    open: open,
    onClose: onClose,
    header: /*#__PURE__*/React.createElement("div", {
      className: "pisell-lowcode__booking-service-header"
    }, /*#__PURE__*/React.createElement(Input, {
      prefix: /*#__PURE__*/React.createElement(SearchLg, null),
      style: {
        marginTop: 16,
        height: 44
      },
      placeholder: "",
      allowClear: true,
      value: search,
      onChange: function onChange(e) {
        setSearch(e.target.value);
      }
    }))
  }, drawerProps), /*#__PURE__*/React.createElement("div", {
    className: "pisell-lowcode__booking-service-body"
  }, /*#__PURE__*/React.createElement(TabProduct, _extends({
    list: renderList
    // scrollDom='.booking-server-wrap .pisell-lowcode-drawer-body'
  }, tabProductProps, {
    renderItem: function renderItem(item) {
      return renderOptionItem === null || renderOptionItem === void 0 ? void 0 : renderOptionItem(item, onSelect);
    }
  })))));
};
export default SelectModal;