import { isFormValue } from "./FormComponent.js";
import { markdown } from "./markdownParser.js";
export function getLocationFieldViewModel(component, viewModel, payload, errors) {
  const {
    collection,
    name
  } = component;
  const {
    fieldset: existingFieldset,
    label
  } = viewModel;

  // Check for component errors only
  const hasError = errors?.some(error => error.name === name);

  // Use the component collection to generate the subitems
  const items = collection.getViewModel(payload, errors).map(({
    model
  }) => {
    let {
      label,
      type,
      value,
      classes,
      prefix,
      suffix,
      errorMessage
    } = model;
    if (label) {
      label.toString = () => label.text; // Use string labels
    }
    if (hasError || errorMessage) {
      classes = `${classes ?? ''} govuk-input--error`.trim();
    }

    // Allow any `toString()`-able value so non-numeric
    // values are shown alongside their error messages
    if (!isFormValue(value)) {
      value = undefined;
    }
    return {
      label,
      id: model.id,
      name: model.name,
      type,
      value,
      classes,
      prefix,
      suffix
    };
  });
  const fieldset = existingFieldset ?? {
    legend: {
      text: label.text,
      classes: 'govuk-fieldset__legend--m'
    }
  };
  const result = {
    ...viewModel,
    fieldset,
    items
  };
  if (component.options.instructionText) {
    return {
      ...result,
      instructionText: markdown.parse(component.options.instructionText, {
        async: false
      })
    };
  }
  return result;
}

/**
 * Validator factory for location-based fields.
 * This creates a validator that ensures all required fields are present.
 */
export function createLocationFieldValidator(component) {
  return (payload, helpers) => {
    const {
      collection,
      name,
      options
    } = component;
    const values = component.getFormValueFromState(component.getStateFromValidForm(payload));
    const context = {
      missing: collection.keys,
      key: name
    };
    if (!component.isState(values)) {
      return options.required !== false ? helpers.error('object.required', context) : payload;
    }
    return payload;
  };
}
//# sourceMappingURL=LocationFieldHelpers.js.map