"use strict";

exports.__esModule = true;
exports.SchemaProvider = void 0;
var _react = _interopRequireWildcard(require("react"));
var _react2 = require("@wix/bex-core/react");
var _SchemaContext = require("./SchemaContext");
var _CmsFieldModalState = require("../components/CmsFieldManagement/CmsFieldModalState");
var _CmsFieldModal = require("../components/CmsFieldManagement/CmsFieldModal");
var _jsxFileName = "/home/builduser/work/843658b9f44192f0/packages/cairo/dist/cjs/providers/SchemaProvider.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); }
const SchemaProvider = ({
  schema,
  children
}) => {
  const container = (0, _react2.useWixPatternsContainer)();
  const schemaRef = (0, _react.useRef)(schema);
  schemaRef.current = schema;
  const modalState = (0, _react.useMemo)(() => new _CmsFieldModalState.CmsFieldModalState(() => schemaRef.current, e => {
    var _container$errorHandl;
    return ((_container$errorHandl = container.errorHandler) == null || _container$errorHandl.getResolvedError == null ? void 0 : _container$errorHandl.getResolvedError(e)) ?? {
      message: e instanceof Error ? e.message : String(e)
    };
  }), [container]);
  const value = (0, _react.useMemo)(() => {
    var _schema$fieldActions, _schema$fieldActions2;
    return {
      schema,
      ...(((_schema$fieldActions = schema.fieldActions) == null ? void 0 : _schema$fieldActions.addField) && {
        addField: () => modalState.open()
      }),
      ...(((_schema$fieldActions2 = schema.fieldActions) == null ? void 0 : _schema$fieldActions2.editField) && {
        editField: fieldId => modalState.open(fieldId)
      })
    };
  }, [schema, modalState]);
  return /*#__PURE__*/_react.default.createElement(_SchemaContext.SchemaContext.Provider, {
    value: value,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 45,
      columnNumber: 5
    }
  }, children, /*#__PURE__*/_react.default.createElement(_CmsFieldModal.CmsFieldModal, {
    state: modalState,
    __self: void 0,
    __source: {
      fileName: _jsxFileName,
      lineNumber: 47,
      columnNumber: 7
    }
  }));
};
exports.SchemaProvider = SchemaProvider;
//# sourceMappingURL=SchemaProvider.js.map