text: "use strict";
  
  Object.defineProperty(exports, "__esModule", {
    value: true
  });
  exports.ObjectType = void 0;
  
  var _isPlainObject2 = _interopRequireDefault(require("lodash/isPlainObject"));
  
  var _startCase2 = _interopRequireDefault(require("lodash/startCase"));
  
  var _keyBy2 = _interopRequireDefault(require("lodash/keyBy"));
  
  var _toPath2 = _interopRequireDefault(require("lodash/toPath"));
  
  var _pick2 = _interopRequireDefault(require("lodash/pick"));
  
  var _createPreviewGetter = _interopRequireDefault(require("../preview/createPreviewGetter"));
  
  var _guessOrderingConfig = _interopRequireDefault(require("../ordering/guessOrderingConfig"));
  
  var _resolveSearchConfig = _interopRequireDefault(require("../resolveSearchConfig"));
  
  var _utils = require("./utils");
  
  var _constants = require("./constants");
  
  var _excluded = ["name", "fieldset"];
  
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  
  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); if (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 = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { 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; }
  
  var OVERRIDABLE_FIELDS = [..._constants.DEFAULT_OVERRIDEABLE_FIELDS, 'orderings', '__experimental_search', 'blockEditor', 'icon'];
  
  var normalizeSearchConfig = configs => {
    if (!Array.isArray(configs)) {
      throw new Error('The search config of a document type must be an array of search config objects');
    }
  
    return configs.map(conf => {
      if (conf === 'defaults') {
        return conf;
      }
  
      if (!(0, _isPlainObject2.default)(conf)) {
        throw new Error('Search config must be an object of {path: string, weight: number}');
      }
  
      if (typeof conf.path !== 'string') {
        throw new Error('The path property of the search field declaration must be a string');
      }
  
      return {
        weight: 'weight' in conf ? conf.weight : 1,
        path: (0, _toPath2.default)(conf.path),
        mapWith: typeof conf.mapWith === 'string' ? conf.mapWith : undefined
      };
    });
  };
  
  var ObjectType = {
    get() {
      return {
        name: 'object',
        type: null,
        jsonType: 'object'
      };
    },
  
    extend(rawSubTypeDef, createMemberType) {
      var subTypeDef = _objectSpread({
        fields: []
      }, rawSubTypeDef);
  
      var options = _objectSpread({}, subTypeDef.options || {});
  
      var parsed = Object.assign((0, _pick2.default)(this.get(), OVERRIDABLE_FIELDS), subTypeDef, {
        type: this.get(),
        title: subTypeDef.title || (subTypeDef.name ? (0, _startCase2.default)(subTypeDef.name) : ''),
        options: options,
        orderings: subTypeDef.orderings || (0, _guessOrderingConfig.default)(subTypeDef),
        fields: subTypeDef.fields.map(fieldDef => {
          var name = fieldDef.name,
              fieldset = fieldDef.fieldset,
              rest = _objectWithoutProperties(fieldDef, _excluded);
  
          var compiledField = {
            name,
            fieldset
          };
          return (0, _utils.lazyGetter)(compiledField, 'type', () => {
            return createMemberType(_objectSpread(_objectSpread({}, rest), {}, {
              title: fieldDef.title || (0, _startCase2.default)(name)
            }));
          });
        })
      });
      (0, _utils.lazyGetter)(parsed, 'fieldsets', () => {
        return createFieldsets(subTypeDef, parsed.fields);
      });
      (0, _utils.lazyGetter)(parsed, 'preview', (0, _createPreviewGetter.default)(subTypeDef));
      (0, _utils.lazyGetter)(parsed, '__experimental_search', () => {
        var userProvidedSearchConfig = subTypeDef.__experimental_search ? normalizeSearchConfig(subTypeDef.__experimental_search) : null;
  
        if (userProvidedSearchConfig) {
          return userProvidedSearchConfig.map(entry => entry === 'defaults' ? (0, _resolveSearchConfig.default)(subTypeDef) : entry);
        }
  
        return (0, _resolveSearchConfig.default)(parsed);
      }, {
        enumerable: false
      });
      return subtype(parsed);
  
      function subtype(parent) {
        return {
          get() {
            return parent;
          },
  
          extend: extensionDef => {
            if (extensionDef.fields) {
              throw new Error('Cannot override `fields` of subtypes of "object"');
            }
  
            var current = Object.assign({}, parent, (0, _pick2.default)(extensionDef, OVERRIDABLE_FIELDS), {
              title: extensionDef.title || subTypeDef.title || (subTypeDef.name ? (0, _startCase2.default)(subTypeDef.name) : ''),
              type: parent
            });
            (0, _utils.lazyGetter)(current, '__experimental_search', () => parent.__experimental_search);
            return subtype(current);
          }
        };
      }
    }
  
  };
  exports.ObjectType = ObjectType;
  
  function createFieldsets(typeDef, fields) {
    var fieldsetsDef = typeDef.fieldsets || [];
    var fieldsets = fieldsetsDef.map(fieldset => {
      var name = fieldset.name,
          title = fieldset.title,
          description = fieldset.description,
          options = fieldset.options,
          hidden = fieldset.hidden,
          readOnly = fieldset.readOnly;
      return {
        name,
        title,
        description,
        options,
        fields: [],
        hidden,
        readOnly
      };
    });
    var fieldsetsByName = (0, _keyBy2.default)(fieldsets, 'name');
    return fields.map(field => {
      if (field.fieldset) {
        var fieldset = fieldsetsByName[field.fieldset];
  
        if (!fieldset) {
          throw new Error("Group '".concat(field.fieldset, "' is not defined in schema for type '").concat(typeDef.name, "'"));
        }
  
        fieldset.fields.push(field); // Return the fieldset if its the first time we encounter a field in this fieldset
  
        return fieldset.fields.length === 1 ? fieldset : null;
      }
  
      return {
        single: true,
        field
      };
    }).filter(Boolean);
  }