"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CmsFieldModalState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _bexCore = require("@wix/bex-core");
var _CmsFormState = require("./CmsFormState");
var _toFieldSpec = require("./toFieldSpec");
var _fieldValidationRulesMap = require("./fieldValidationRulesMap");
class CmsFieldModalState {
  constructor(getSchema, resolveError) {
    (0, _defineProperty2.default)(this, "getSchema", void 0);
    (0, _defineProperty2.default)(this, "resolveError", void 0);
    (0, _defineProperty2.default)(this, "modal", void 0);
    (0, _defineProperty2.default)(this, "formState", null);
    this.getSchema = getSchema;
    this.resolveError = resolveError;
    this.modal = new _bexCore.ConditionalModalState({
      onConfirm: () => this.submit()
    });
    (0, _mobx.makeObservable)(this, {
      formState: _mobx.observable.ref,
      open: _mobx.action,
      reset: _mobx.action
    });
  }
  open(fieldId) {
    const schemaConfig = this.getSchema();
    this.formState = new _CmsFormState.CmsFormState({
      schemaConfig,
      getValidationRules: _fieldValidationRulesMap.getFieldTypeValidationRulesList
    });
    if (fieldId) {
      const field = schemaConfig.fields[fieldId];
      if (!field) {
        console.warn(`[CmsFieldModalState] Missing field "${fieldId}"`);
        return;
      }
      this.formState.setMode('edit');
      this.formState.loadField(field);
    }
    this.modal.open(null);
  }
  reset() {
    this.formState = null;
  }
  async submit() {
    const {
      formState
    } = this;
    if (!formState) {
      return;
    }
    const schemaConfig = this.getSchema();
    if (formState.hasInvalidFields || !formState.properties.type) {
      formState.enableShowValidationError();
      throw new Error('validation');
    }
    try {
      var _schemaConfig$fieldAc2;
      const {
        type,
        ...rest
      } = formState.properties;
      const fieldSpec = (0, _toFieldSpec.toFieldSpec)({
        ...rest,
        type
      }, formState.fieldRules);
      if (formState.mode === 'edit') {
        var _schemaConfig$fieldAc;
        if (!formState.editingFieldId) {
          throw new Error('Missing editing field id for CMS field update');
        }
        if (!((_schemaConfig$fieldAc = schemaConfig.fieldActions) != null && _schemaConfig$fieldAc.editField)) {
          throw new Error('SchemaConfig.fieldActions.editField is not configured');
        }
        await schemaConfig.fieldActions.editField(formState.editingFieldId, fieldSpec);
        schemaConfig.refresh == null || schemaConfig.refresh();
        return;
      }
      if (!((_schemaConfig$fieldAc2 = schemaConfig.fieldActions) != null && _schemaConfig$fieldAc2.addField)) {
        throw new Error('SchemaConfig.fieldActions.addField is not configured');
      }
      await schemaConfig.fieldActions.addField(fieldSpec);
      schemaConfig.refresh == null || schemaConfig.refresh();
    } catch (e) {
      formState.setErrorToastConfig(this.resolveError(e));
      throw e;
    }
  }
}
exports.CmsFieldModalState = CmsFieldModalState;
//# sourceMappingURL=CmsFieldModalState.js.map