var _excluded = ["children", "title", "xIndex"],
  _excluded2 = ["children"];
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
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; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
import React from 'react';
import cls from 'classnames';
import { observer, useFieldSchema, useField, RecursionField } from '@formily/react';
import { usePrefixCls } from '@teamix/utils';
import './index.scss';
var usePanels = function usePanels() {
  var groupField = useField();
  var schema = useFieldSchema();
  var panels = [];
  schema.mapProperties(function (schema, name) {
    var _schema$xComponent;
    var field = groupField.query(groupField.address.concat(name)).take();
    if ((field === null || field === void 0 ? void 0 : field.display) === 'none' || (field === null || field === void 0 ? void 0 : field.display) === 'hidden') return;
    if (((_schema$xComponent = schema['x-component']) === null || _schema$xComponent === void 0 ? void 0 : _schema$xComponent.indexOf('GroupPanel')) > -1) {
      var _schema$xComponentP, _schema$xComponentP2;
      panels.push({
        name: name,
        props: _objectSpread(_objectSpread({}, schema === null || schema === void 0 ? void 0 : schema['x-component-props']), {}, {
          title: (schema === null || schema === void 0 ? void 0 : (_schema$xComponentP = schema['x-component-props']) === null || _schema$xComponentP === void 0 ? void 0 : _schema$xComponentP.title) || (schema === null || schema === void 0 ? void 0 : schema.title),
          key: (schema === null || schema === void 0 ? void 0 : (_schema$xComponentP2 = schema['x-component-props']) === null || _schema$xComponentP2 === void 0 ? void 0 : _schema$xComponentP2.key) || name
        }),
        schema: schema
      });
    }
  });
  return panels;
};
var FormGroupGroupPanel = observer(function (_ref) {
  var children = _ref.children,
    title = _ref.title,
    xIndex = _ref.xIndex,
    props = _objectWithoutProperties(_ref, _excluded);
  var prefixCls = usePrefixCls('formily-group-panel', {
    base: true
  });
  return /*#__PURE__*/React.createElement("div", _objectSpread(_objectSpread({}, props), {}, {
    className: cls(prefixCls(), props.className)
  }), xIndex !== 0 ? /*#__PURE__*/React.createElement("div", {
    className: prefixCls('divider')
  }) : null, /*#__PURE__*/React.createElement("div", {
    className: prefixCls('title')
  }, title), children);
});
var FormGroup = observer(function (_ref2) {
  var children = _ref2.children,
    props = _objectWithoutProperties(_ref2, _excluded2);
  var prefixCls = usePrefixCls('formily-group', {
    base: true
  });
  var panels = usePanels();
  return /*#__PURE__*/React.createElement("div", _objectSpread(_objectSpread({}, props), {}, {
    className: cls(prefixCls(), props.className)
  }), panels.map(function (_ref3, index) {
    var props = _ref3.props,
      schema = _ref3.schema,
      name = _ref3.name;
    return /*#__PURE__*/React.createElement(FormGroupGroupPanel, _objectSpread({
      key: index,
      xIndex: index
    }, props), /*#__PURE__*/React.createElement(RecursionField, {
      schema: schema,
      name: name
    }));
  }));
});
var GroupPanel = function GroupPanel(_ref4) {
  var children = _ref4.children;
  return /*#__PURE__*/React.createElement(React.Fragment, null, children);
};
FormGroup.GroupPanel = GroupPanel;
export default FormGroup;