"use strict";

exports.__esModule = true;
exports.useFormPageContent = useFormPageContent;
var _react = _interopRequireWildcard(require("react"));
var _designSystem = require("@wix/design-system");
var _FormPageMainContent = require("../../components/FormPageMainContent");
var _FormPageAdditionalContent = require("../../components/FormPageAdditionalContent");
var _FormPage = require("../../components/FormPage");
var _useAutoScrollOnLoad = require("../../hooks/useAutoScrollOnLoad");
var _bexUtils = require("@wix/bex-utils");
var _FormFooter = require("./FormFooter");
var _useIsMobile = require("../../hooks/useIsMobile");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/state/FormPage/useFormPageContent.tsx";
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function useFormPageContent(child, {
  elementName = 'FormPage',
  MainContentElement = _FormPageMainContent.FormPageMainContent,
  AdditionalContentElement = _FormPageAdditionalContent.FormPageAdditionalContent,
  FooterElement = _FormPage.FormPageActionsBar,
  scrollableRef
} = {}) {
  const childrenArray = _react.Children.toArray(child.props.children);
  const scrollToItem = (0, _react.useRef)();
  const doesItemExist = (0, _react.useRef)();
  if (process.env.NODE_ENV !== 'production') {
    if (!childrenArray.every(elem => /*#__PURE__*/(0, _react.isValidElement)(elem) && (elem.type === MainContentElement || elem.type === AdditionalContentElement))) {
      throw new Error(`<${elementName}.Content/> must have only <${elementName}.MainContent/> and <${elementName}.AdditionalContent/> children`);
    }
    if (childrenArray.length === 0 || childrenArray.length > 2) {
      throw new Error(`<${elementName}.Content/> must have 1 (<${elementName}.MainContent/>) or 2 (<${elementName}.MainContent/> and <${elementName}.AdditionalContent/>) children`);
    }
    if (! /*#__PURE__*/(0, _react.isValidElement)(childrenArray[0]) || childrenArray[0].type !== MainContentElement) {
      throw new Error(`<${elementName}.MainContent/> should be the first child of <${elementName}.Content/>`);
    }
    if (childrenArray.length === 2 && (! /*#__PURE__*/(0, _react.isValidElement)(childrenArray[1]) || childrenArray[1].type !== AdditionalContentElement)) {
      throw new Error(`Only <${elementName}.AdditionalContent/> can be the second child of <${elementName}.Content/>`);
    }
  }
  if (/*#__PURE__*/(0, _react.isValidElement)(childrenArray[0])) {
    childrenArray[0] = /*#__PURE__*/(0, _react.cloneElement)(childrenArray[0], {
      span: childrenArray.length === 1 ? 12 : 8
    }, childrenArray[0].props.children);
  }
  (0, _useAutoScrollOnLoad.useAutoScrollOnLoad)(scrollToItem, doesItemExist);
  const isMobile = (0, _useIsMobile.useIsMobile)();
  return /*#__PURE__*/_react.default.createElement(_designSystem.Page.Content, {
    key: child.key,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 94,
      columnNumber: 5
    }
  }, /*#__PURE__*/_react.default.createElement(_bexUtils.FocusableWrapper, {
    scrollableRef: scrollableRef,
    getScrollableTrigger: triggerScrollable => {
      scrollToItem.current = triggerScrollable;
    },
    getDoesItemExist: callback => {
      doesItemExist.current = callback;
    },
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 95,
      columnNumber: 7
    }
  }, /*#__PURE__*/_react.default.createElement(_designSystem.Layout, {
    cols: isMobile ? 1 : undefined,
    gap: isMobile ? '12px 0' : undefined,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 104,
      columnNumber: 9
    }
  }, childrenArray, !isMobile && /*#__PURE__*/_react.default.createElement(_designSystem.Cell, {
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 110,
      columnNumber: 13
    }
  }, /*#__PURE__*/_react.default.createElement(_FormFooter.FormFooter, {
    scrollableRef: scrollableRef,
    FooterElement: FooterElement,
    __self: this,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 111,
      columnNumber: 15
    }
  })))));
}
//# sourceMappingURL=useFormPageContent.js.map