import { ComponentType, hasFormComponents, isFormType } from '@defra/forms-model';
import joi from 'joi';
import { FormComponent, isFormValue } from "./FormComponent.js";
import { messageTemplate } from "../pageControllers/validationOptions.js";
export class DeclarationField extends FormComponent {
  DEFAULT_DECLARATION_LABEL = 'I understand and agree';
  headerStartLevel;
  constructor(def, props) {
    super(def, props);
    const {
      options,
      content
    } = def;
    let checkboxSchema = joi.string().valid('true');
    if (options.required !== false) {
      checkboxSchema = checkboxSchema.required();
    }
    const formSchema = joi.array().items(checkboxSchema, joi.string().valid('unchecked').strip()).label(this.label).single().messages({
      'any.required': messageTemplate.declarationRequired,
      'any.unknown': messageTemplate.declarationRequired,
      'array.includesRequiredUnknowns': messageTemplate.declarationRequired
    });
    this.formSchema = formSchema;
    this.stateSchema = joi.boolean().cast('string').label(this.label).required();
    this.options = options;
    this.content = content;
    this.declarationConfirmationLabel = options.declarationConfirmationLabel ?? this.DEFAULT_DECLARATION_LABEL;
    const formComponents = hasFormComponents(props.page?.pageDef) ? props.page.pageDef.components : [];
    const numOfQuestionsOnPage = formComponents.filter(q => isFormType(q.type)).length;
    const hasGuidance = formComponents.some((comp, idx) => comp.type === ComponentType.Markdown && idx === 0);
    this.headerStartLevel = numOfQuestionsOnPage < 2 && !hasGuidance ? 2 : 3;
  }
  getFormValueFromState(state) {
    const {
      name
    } = this;
    return state[name] === true ? 'true' : 'false';
  }
  getFormDataFromState(state) {
    const {
      name
    } = this;
    return {
      [name]: state[name] === true ? 'true' : 'false'
    };
  }
  getStateFromValidForm(payload) {
    const {
      name
    } = this;
    const payloadValue = payload[name];
    const value = this.isValue(payloadValue) && payloadValue.length > 0 && payloadValue.every(v => {
      return v === 'true';
    });
    return {
      [name]: value
    };
  }
  getContextValueFromFormValue(value) {
    return value === 'true';
  }
  getFormValue(value) {
    return this.isValue(value) ? value : undefined;
  }
  getDisplayStringFromFormValue(value) {
    return value === 'true' ? this.declarationConfirmationLabel : 'Not provided';
  }
  getViewModel(payload, errors) {
    const defaultDeclarationConfirmationLabel = 'I confirm that I understand and accept this declaration';
    const {
      hint,
      content,
      declarationConfirmationLabel = defaultDeclarationConfirmationLabel
    } = this;
    const viewModel = super.getViewModel(payload, errors);
    let {
      fieldset,
      label
    } = viewModel;
    fieldset ??= {
      legend: {
        text: label.text,
        classes: 'govuk-fieldset__legend--m'
      }
    };
    const payloadValue = payload[this.name];
    const isChecked = payloadValue === 'true' || payloadValue === true || Array.isArray(payloadValue) && payloadValue.some(x => x === 'true');
    return {
      ...viewModel,
      hint: hint ? {
        text: hint
      } : undefined,
      fieldset,
      content,
      items: [{
        text: declarationConfirmationLabel,
        value: 'true',
        checked: isChecked
      }],
      headerStartLevel: this.headerStartLevel
    };
  }
  isValue(value) {
    if (!Array.isArray(value)) {
      return false;
    }

    // Skip checks when empty
    if (!value.length) {
      return true;
    }
    return value.every(isFormValue);
  }

  /**
   * For error preview page that shows all possible errors on a component
   */
  getAllPossibleErrors() {
    return DeclarationField.getAllPossibleErrors();
  }

  /**
   * Static version of getAllPossibleErrors that doesn't require a component instance.
   */
  static getAllPossibleErrors() {
    return {
      baseErrors: [{
        type: 'required',
        template: messageTemplate.declarationRequired
      }],
      advancedSettingsErrors: []
    };
  }
  static isBool(value) {
    return isFormValue(value) && typeof value === 'boolean';
  }
}
//# sourceMappingURL=DeclarationField.js.map