"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports.CmsFormState = void 0;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _mobx = require("mobx");
var _fieldTypeUtils = require("./fieldTypeUtils");
function defaultProperties() {
  return {
    type: undefined,
    name: '',
    isPii: false,
    helpText: ''
  };
}

// TODO: confirm vs cms-web
const NAME_MAX_LENGTH = 50;
class CmsFormState {
  constructor(params) {
    (0, _defineProperty2.default)(this, "schemaConfig", void 0);
    (0, _defineProperty2.default)(this, "getValidationRules", void 0);
    (0, _defineProperty2.default)(this, "properties", defaultProperties());
    (0, _defineProperty2.default)(this, "fieldRules", {
      validationRules: {}
    });
    (0, _defineProperty2.default)(this, "mode", void 0);
    (0, _defineProperty2.default)(this, "editingFieldId", void 0);
    (0, _defineProperty2.default)(this, "touchedFields", new Set());
    (0, _defineProperty2.default)(this, "showValidationError", false);
    (0, _defineProperty2.default)(this, "errorToastConfig", undefined);
    this.schemaConfig = params.schemaConfig;
    this.getValidationRules = params.getValidationRules;
    this.mode = params.mode ?? 'new';
    this.editingFieldId = params.editingFieldId;
    (0, _mobx.makeObservable)(this, {
      properties: _mobx.observable.ref,
      fieldRules: _mobx.observable.ref,
      mode: _mobx.observable.ref,
      editingFieldId: _mobx.observable.ref,
      touchedFields: _mobx.observable,
      showValidationError: _mobx.observable.ref,
      errorToastConfig: _mobx.observable.ref,
      fieldTypeError: _mobx.computed,
      nameError: _mobx.computed,
      hasInvalidFields: _mobx.computed,
      visibleErrors: _mobx.computed,
      supportedValidationRules: _mobx.computed,
      validationErrorMessage: _mobx.computed,
      setProperty: _mobx.action,
      setValidationRule: _mobx.action.bound,
      blurField: _mobx.action,
      enableShowValidationError: _mobx.action,
      setMode: _mobx.action,
      setErrorToastConfig: _mobx.action,
      loadField: _mobx.action,
      reset: _mobx.action,
      hideValidationError: _mobx.action.bound,
      changeType: _mobx.action.bound,
      changeName: _mobx.action.bound,
      setIsPii: _mobx.action.bound,
      setHelpText: _mobx.action.bound
    });
  }
  get fieldTypeError() {
    if (!this.properties.type) {
      return 'Field type is required';
    }
    return undefined;
  }
  get nameError() {
    const name = this.properties.name.trim();
    if (!name) {
      return 'Field name is required';
    }
    if (name.length > NAME_MAX_LENGTH) {
      return `Field name must be ${NAME_MAX_LENGTH} characters or fewer`;
    }
    return undefined;
  }
  get hasInvalidFields() {
    return !!(this.fieldTypeError || this.nameError);
  }

  // Spec §3 specifies booleans; returning the error string directly is more useful for rendering
  // (one property read instead of two). Deviation is intentional — update spec if accepted.
  get visibleErrors() {
    const {
      showValidationError,
      touchedFields
    } = this;
    return {
      type: showValidationError ? this.fieldTypeError : undefined,
      name: showValidationError || touchedFields.has('name') ? this.nameError : undefined
    };
  }
  get supportedValidationRules() {
    return this.properties.type ? this.getValidationRules(this.properties.type) : [];
  }
  get validationErrorMessage() {
    return this.fieldTypeError || this.nameError || 'Please fix the errors before saving';
  }
  setProperty(key, value) {
    this.properties = {
      ...this.properties,
      [key]: value
    };
  }
  setValidationRule(rule, value) {
    this.fieldRules = {
      ...this.fieldRules,
      validationRules: {
        ...this.fieldRules.validationRules,
        [rule]: value
      }
    };
  }
  blurField(field) {
    this.touchedFields.add(field);
  }
  enableShowValidationError() {
    this.showValidationError = true;
  }
  setMode(mode) {
    this.mode = mode;
  }
  setErrorToastConfig(cfg) {
    this.errorToastConfig = cfg;
  }
  reset() {
    this.properties = defaultProperties();
    this.fieldRules = {
      validationRules: {}
    };
    this.editingFieldId = undefined;
    this.touchedFields.clear();
    this.showValidationError = false;
    this.errorToastConfig = undefined;
  }
  hideValidationError() {
    this.showValidationError = false;
  }
  changeType(v) {
    this.setProperty('type', v);
    this._onTypeChange(v);
  }
  changeName(v) {
    this.setProperty('name', v);
  }
  setIsPii(v) {
    this.setProperty('isPii', v);
  }
  setHelpText(v) {
    this.setProperty('helpText', v);
  }
  loadField(field) {
    var _field$validation, _field$validation2;
    const mappedType = (0, _fieldTypeUtils.isSelectableFieldType)(field.type) ? field.type : undefined;
    this.properties = {
      type: mappedType,
      name: field.displayName,
      isPii: field.isPii ?? false,
      helpText: field.helpText ?? ''
    };
    this.fieldRules = {
      validationRules: {
        ...(((_field$validation = field.validation) == null ? void 0 : _field$validation.required) !== undefined && {
          required: field.validation.required
        }),
        ...(((_field$validation2 = field.validation) == null ? void 0 : _field$validation2.stringLengthRange) !== undefined && {
          charactersLimit: field.validation.stringLengthRange
        })
      }
    };
    this.editingFieldId = field.id;
    this.errorToastConfig = undefined;
  }
  _onTypeChange(newType) {
    const supported = new Set(this.getValidationRules(newType));
    this.fieldRules = {
      validationRules: Object.fromEntries(Object.entries(this.fieldRules.validationRules).filter(([k]) => supported.has(k)))
    };
    if (!(0, _fieldTypeUtils.isPIISupportedByFieldType)(newType)) {
      this.properties = {
        ...this.properties,
        isPii: false
      };
    }
    this.touchedFields.clear();
  }
}
exports.CmsFormState = CmsFormState;
//# sourceMappingURL=CmsFormState.js.map