"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CustomFieldsAllNamespacesFormState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _orderBy = _interopRequireDefault(require("lodash/orderBy"));
class CustomFieldsAllNamespacesFormState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "_customFieldsWidgetState", void 0);
    (0, _defineProperty2.default)(this, "namespacesFormsMap", _mobx.observable.map([], {
      deep: false
    }));
    this._customFieldsWidgetState = params.customFieldsWidgetState;
    (0, _mobx.makeObservable)(this, {
      namespacesForms: _mobx.computed,
      addFormIfAbsent: _mobx.action,
      validate: _mobx.action,
      isDirty: _mobx.computed,
      schemasSections: _mobx.computed,
      tpaFieldsExist: _mobx.computed
    });
  }
  get schemasSections() {
    const {
      _customFieldsWidgetState: {
        dataExtension: {
          _dataExtensionSchemas
        },
        _userFieldsNamespace
      }
    } = this;
    const schemasWithEditableProps = _dataExtensionSchemas.filter(schema => {
      var _schema$jsonSchema;
      return ((_schema$jsonSchema = schema.jsonSchema) == null ? void 0 : _schema$jsonSchema.properties) && Object.keys(schema.jsonSchema.properties).length > 0 && (schema.namespace === _userFieldsNamespace || Object.values(schema.jsonSchema.properties).some(prop => {
        var _prop$xWixPermissio, _prop$xWixPermissio2;
        return ((_prop$xWixPermissio = prop['x-wix-permissions']) == null || (_prop$xWixPermissio = _prop$xWixPermissio.write) == null ? void 0 : _prop$xWixPermissio.includes('users')) && ((_prop$xWixPermissio2 = prop['x-wix-permissions']) == null || (_prop$xWixPermissio2 = _prop$xWixPermissio2.read) == null ? void 0 : _prop$xWixPermissio2.includes('users'));
      }));
    });
    return (0, _orderBy.default)(schemasWithEditableProps, [s => s.namespace === _userFieldsNamespace ? 1 : 0, s => s.createdDate], ['desc', 'desc']);
  }
  get tpaFieldsExist() {
    const {
      schemasSections,
      _customFieldsWidgetState: {
        _userFieldsNamespace
      }
    } = this;
    return schemasSections.some(schema => schema.namespace !== _userFieldsNamespace);
  }
  getNamespaceFormSchemaAndData(namespace) {
    var _state$_extendedField;
    const {
      _customFieldsWidgetState: state
    } = this;
    const {
      schema,
      fields
    } = state.dataExtension.getSchemaAndFieldsByNamespace(namespace);
    return {
      fields,
      schema,
      extendedFieldsDataMap: (_state$_extendedField = state._extendedFields) == null || (_state$_extendedField = _state$_extendedField.namespaces) == null ? void 0 : _state$_extendedField[namespace]
    };
  }
  get namespacesForms() {
    return Array.from(this.namespacesFormsMap.values());
  }
  addFormIfAbsent(form) {
    const {
      namespacesFormsMap
    } = this;
    const maybeForm = namespacesFormsMap.get(form._namespace) ?? form;
    namespacesFormsMap.set(form._namespace, maybeForm);
    return maybeForm;
  }
  validate() {
    const {
      namespacesFormsMap,
      _customFieldsWidgetState: {
        _userFieldsNamespace
      }
    } = this;
    const resultsMap = new Map(Array.from(namespacesFormsMap.values()).map(form => [form._namespace, form.validate()]));
    const results = Array.from(resultsMap.values());
    const extendedFields = Object.fromEntries(Array.from(resultsMap.entries()).map(([namespace, result]) => [namespace, result.extendedFields]));
    return {
      isValid: results.every(result => result.isValid),
      extendedFields: {
        namespaces: {
          [_userFieldsNamespace]: {},
          ...extendedFields
        }
      }
    };
  }
  get isDirty() {
    const {
      namespacesForms
    } = this;
    return namespacesForms.some(form => form.isDirty);
  }
}
exports.CustomFieldsAllNamespacesFormState = CustomFieldsAllNamespacesFormState;
//# sourceMappingURL=CustomFieldsAllNamespacesFormState.js.map