function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import React, { useMemo } from 'react';
import { Empty, Spin, TreeSelect } from 'antd';
import { useRequest } from 'ahooks';
import { locales } from '@pisell/utils';
import { getResourceTypes } from "../serve";
var formatResourceNode = function formatResourceNode(item) {
  var _ref, _ref2, _ref3, _ref4, _ref5, _item$id, _ref6, _item$name, _item$resources;
  var key = (_ref = (_ref2 = (_ref3 = (_ref4 = (_ref5 = (_item$id = item.id) !== null && _item$id !== void 0 ? _item$id : item.value) !== null && _ref5 !== void 0 ? _ref5 : item.code) !== null && _ref4 !== void 0 ? _ref4 : item.name) !== null && _ref3 !== void 0 ? _ref3 : item.title) !== null && _ref2 !== void 0 ? _ref2 : item.label) !== null && _ref !== void 0 ? _ref : '';
  return {
    key: key,
    value: key,
    title: (_ref6 = (_item$name = item.name) !== null && _item$name !== void 0 ? _item$name : item.title) !== null && _ref6 !== void 0 ? _ref6 : item.label,
    children: formatResourceNodes((_item$resources = item.resources) !== null && _item$resources !== void 0 ? _item$resources : item.children)
  };
};
var formatResourceNodes = function formatResourceNodes(items) {
  if (!items) return [];
  return items.map(formatResourceNode);
};
var buildTreeData = function buildTreeData(list) {
  console.log('xxxxxx', list);
  var formatted = (list || []).map(function (item) {
    var _item$resources2;
    return _objectSpread(_objectSpread({}, item), {}, {
      resources: (_item$resources2 = item.resources) !== null && _item$resources2 !== void 0 ? _item$resources2 : item.children
    });
  });
  formatted.unshift({
    id: 0,
    name: locales.getText('pisell2.salesManagement.booking.filter.resource.none'),
    resources: []
  });
  return formatResourceNodes(formatted);
};
var normalizeValue = function normalizeValue(value) {
  if (Array.isArray(value)) return value;
  if (value === undefined || value === null) return [];
  return [value];
};
var extractLeafKeys = function extractLeafKeys(nodes) {
  if (!(nodes !== null && nodes !== void 0 && nodes.length)) return [];
  return nodes.filter(function (node) {
    return !(node !== null && node !== void 0 && node.children) || node.children.length === 0;
  }).map(function (node) {
    var _node$key;
    return (_node$key = node.key) !== null && _node$key !== void 0 ? _node$key : node.value;
  }).filter(function (key) {
    return key !== undefined && key !== null;
  });
};
var ResourceSelector = function ResourceSelector(props) {
  var value = props.value,
    _onChange = props.onChange,
    className = props.className,
    style = props.style;
  var _useRequest = useRequest(getResourceTypes),
    data = _useRequest.data,
    loading = _useRequest.loading;
  var treeData = useMemo(function () {
    return buildTreeData(data || []);
  }, [data]);
  var checkedKeys = useMemo(function () {
    return normalizeValue(value);
  }, [value]);
  return /*#__PURE__*/React.createElement("div", {
    className: className,
    style: style
  }, loading ? /*#__PURE__*/React.createElement(Spin, {
    size: "small"
  }) : treeData.length ? /*#__PURE__*/React.createElement(TreeSelect, {
    maxTagCount: 'responsive',
    value: checkedKeys,
    treeData: treeData,
    treeCheckable: true,
    showCheckedStrategy: TreeSelect.SHOW_CHILD,
    treeNodeFilterProp: "title",
    allowClear: true,
    multiple: true,
    style: {
      width: '100%'
    },
    dropdownStyle: {
      maxHeight: 320,
      overflow: 'auto'
    },
    onChange: function onChange(nextValue, _label, extra) {
      var leafKeys = extractLeafKeys(extra === null || extra === void 0 ? void 0 : extra.allCheckedNodes);
      var normalizedKeys = Array.isArray(nextValue) ? nextValue : nextValue != null ? [nextValue] : [];
      _onChange === null || _onChange === void 0 || _onChange(leafKeys.length ? leafKeys : normalizedKeys);
    },
    placeholder: locales.getText('pisell2.salesManagement.booking.filter.resource.placeholder')
  }) : /*#__PURE__*/React.createElement(Empty, {
    image: Empty.PRESENTED_IMAGE_SIMPLE
  }));
};
export default ResourceSelector;