import { FormComponent, isGeospatialState } from "./FormComponent.js";
import { getGeospatialSchema } from "./helpers/geospatial.js";
import { messageTemplate } from "../pageControllers/validationOptions.js";
export class GeospatialField extends FormComponent {
  constructor(def, props) {
    super(def, props);
    const {
      options
    } = def;
    const formSchema = getGeospatialSchema(def).label(this.label).messages({
      'array.min': messageTemplate.featuresMin,
      'array.max': messageTemplate.featuresMax,
      'array.length': messageTemplate.featuresLength
    });
    this.formSchema = formSchema;
    this.stateSchema = formSchema.default(null);
    if (options.required === false) {
      this.stateSchema = this.stateSchema.allow(null);
    }
    this.options = options;
  }
  getFormValueFromState(state) {
    const {
      name
    } = this;
    return this.getFormValue(state[name]);
  }
  getFormValue(value) {
    return this.isValue(value) ? value : undefined;
  }
  getDisplayStringFromFormValue(features) {
    if (!features?.length) {
      return '';
    }
    const unit = features.length === 1 ? 'location' : 'locations';
    return `Added ${features.length} ${unit}`;
  }
  getDisplayStringFromState(state) {
    const features = this.getFormValueFromState(state);
    return this.getDisplayStringFromFormValue(features);
  }
  getContextValueFromFormValue(features) {
    return features?.map(({
      id
    }) => id) ?? null;
  }
  getContextValueFromState(state) {
    const features = this.getFormValueFromState(state);
    return this.getContextValueFromFormValue(features);
  }
  getViewModel(payload, errors) {
    const viewModel = super.getViewModel(payload, errors);
    const value = typeof viewModel.value === 'string' ? viewModel.value : JSON.stringify(viewModel.value, null, 2);
    return {
      ...viewModel,
      country: this.options.countries?.at(0),
      geometryTypes: this.options.geometryTypes,
      value
    };
  }
  getErrors(errors) {
    const fieldErrors = super.getErrors(errors);
    fieldErrors?.forEach(err => {
      if (err.name === 'description') {
        err.href = `#description_${err.path[1]}`;
        err.text = `Enter description for location ${Number(err.path[1]) + 1}`;
      } else if (typeof err.name === 'number' && err.context?.country) {
        err.href = `#description_${err.path[1]}`;
        err.text = `Location ${Number(err.path[1]) + 1} must be in ${err.context.country}`;
      }
    });
    return fieldErrors;
  }
  getViewErrors(errors) {
    return this.getErrors(errors);
  }
  isValue(value) {
    return isGeospatialState(value);
  }

  /**
   * For error preview page that shows all possible errors on a component
   */
  getAllPossibleErrors() {
    const staticErrors = GeospatialField.getAllPossibleErrors();
    return {
      ...staticErrors,
      advancedSettingsErrors: [...staticErrors.advancedSettingsErrors]
    };
  }

  /**
   * Static version of getAllPossibleErrors that doesn't require a component instance.
   */
  static getAllPossibleErrors() {
    return {
      baseErrors: [{
        type: 'required',
        template: messageTemplate.selectRequired
      }, {
        type: 'array.min',
        template: '{{#title}} must contain at least 1 items'
      }, {
        type: 'object.invalidjson',
        template: messageTemplate.format
      }],
      advancedSettingsErrors: []
    };
  }
}
//# sourceMappingURL=GeospatialField.js.map