<commit_before>import React, { useEffect, useState } from 'react';
import * as Sentry from '@sentry/browser';
import moment from 'moment';

import { transformForSubmit } from 'platform/forms-system/src/js/helpers';
import { apiRequest } from 'platform/utilities/api';

function checkStatus(guid) {
  const headers = { 'Content-Type': 'application/json' };

  return apiRequest(`/burial_claims/${guid}`, {
    headers,
    mode: 'cors',
  }).catch(res => {
    if (res instanceof Error) {
      Sentry.captureException(res);
      Sentry.captureMessage('vets_burial_poll_client_error');

      // keep polling because we know they submitted earlier
      // and this is likely a network error
      return Promise.resolve();
    }

    // if we get here, it's likely that we hit a server error
    return Promise.reject(res);
  });
}

const POLLING_INTERVAL = 1000;

function pollStatus(
  { guid, confirmationNumber, regionalOffice },
  onDone,
  onError,
) {
  setTimeout(() => {
    checkStatus(guid)
      .then(res => {
        if (!res || res.data.attributes.state === 'pending') {
          pollStatus(
            { guid, confirmationNumber, regionalOffice },
            onDone,
            onError,
          );
        } else if (res.data.attributes.state === 'success') {
          const response = res.data.attributes.response || {
            confirmationNumber,
            regionalOffice,
          };
          onDone(response);
        } else {
          // needs to start with this string to get the right message on the form
          throw new Error(
            `vets_server_error_burial: status ${res.data.attributes.state}`,
          );
        }
      })
      .catch(onError);
  }, window.VetsGov.pollTimeout || POLLING_INTERVAL);
}

function transformCountryCode(countryCode) {
  switch (countryCode) {
    case 'USA':
      return 'US';
    case 'MEX':
      return 'MX';
    case 'CAN':
      return 'CA';
    default:
      return countryCode;
  }
}

export function transform(formConfig, form) {
  const correctedForm = {
    ...form,
    data: {
      ...form?.data,
      claimantAddress: {
        ...form?.data?.claimantAddress,
        country: transformCountryCode(form?.data?.claimantAddress?.country),
      },
    },
  };
  const formData = transformForSubmit(formConfig, correctedForm);
  return JSON.stringify({
    burialClaim: {
      // this is ugly but the address schema helper within the forms system coerces all country codes back to alpha 3
      form: formData,
    },
    // can’t use toISOString because we need the offset
    localTime: moment().format('Y-MM-DD[T]kk:mm:ssZZ'),
  });
}

export function submit(form, formConfig) {
  const headers = { 'Content-Type': 'application/json' };

  const body = transform(formConfig, form);
  const apiRequestOptions = {
    headers,
    body,
    method: 'POST',
    mode: 'cors',
  };

  const onSuccess = resp => {
    const { guid, confirmationNumber, regionalOffice } = resp.data.attributes;
    return new Promise((resolve, reject) => {
      pollStatus(
        { guid, confirmationNumber, regionalOffice },
        response => {
          window.dataLayer.push({
            event: `${formConfig.trackingPrefix}-submission-successful`,
          });
          return resolve(response);
        },
        error => reject(error),
      );
    });
  };

  const onFailure = respOrError => {
    if (respOrError instanceof Response && respOrError.status === 429) {
      const error = new Error('vets_throttled_error_burial');
      error.extra = parseInt(respOrError.headers.get('x-ratelimit-reset'), 10);

      return Promise.reject(error);
    }
    return Promise.reject(respOrError);
  };

  return apiRequest('/burial_claims', apiRequestOptions)
    .then(onSuccess)
    .catch(onFailure);
}
export const serviceRecordNotification = (
  <div className="usa-alert usa-alert-warning background-color-only">
    <span>
      <strong>Note:</strong> If you would rather upload a DD214 than enter dates
      here, you can do that later in the form.
    </span>
  </div>
);

export const serviceRecordWarning = (
  <div className="usa-alert usa-alert-warning background-color-only">
    <span>
      <strong>Note:</strong> If you chose to upload a DD214 instead of recording
      service periods, you can do that here
    </span>
  </div>
);

export const transportationWarning = (
  <div className="usa-alert usa-alert-warning background-color-only">
    <span>
      <strong>Note:</strong> At the end of the application, you will be asked to
      upload documentation for the expenses you incurred for transporting the
      Veteran’s remains.
    </span>
  </div>
);

export const BurialDateWarning = () => {
  const [shouldRender, setShouldRender] = useState(false);

  useEffect(() => {
    // NOTE: If we don't wait at least 900ms to render,
    // the alert content gets overspoken by the year content
    // when using a Screen Reader.
    // Using 1000ms to give a bit of padding
    const timeout = setTimeout(() => setShouldRender(true), 1000);
    return () => clearTimeout(timeout);
  }, []);

  return (
    <div aria-live="polite">
      {shouldRender && (
        <va-alert background-only show-icon status="warning"><commit_msg>Add screen reader announcement to transportation note (#21943)<commit_after>import React, { useEffect, useState } from 'react';
import * as Sentry from '@sentry/browser';
import moment from 'moment';

import { transformForSubmit } from 'platform/forms-system/src/js/helpers';
import { apiRequest } from 'platform/utilities/api';

function checkStatus(guid) {
  const headers = { 'Content-Type': 'application/json' };

  return apiRequest(`/burial_claims/${guid}`, {
    headers,
    mode: 'cors',
  }).catch(res => {
    if (res instanceof Error) {
      Sentry.captureException(res);
      Sentry.captureMessage('vets_burial_poll_client_error');

      // keep polling because we know they submitted earlier
      // and this is likely a network error
      return Promise.resolve();
    }

    // if we get here, it's likely that we hit a server error
    return Promise.reject(res);
  });
}

const POLLING_INTERVAL = 1000;

function pollStatus(
  { guid, confirmationNumber, regionalOffice },
  onDone,
  onError,
) {
  setTimeout(() => {
    checkStatus(guid)
      .then(res => {
        if (!res || res.data.attributes.state === 'pending') {
          pollStatus(
            { guid, confirmationNumber, regionalOffice },
            onDone,
            onError,
          );
        } else if (res.data.attributes.state === 'success') {
          const response = res.data.attributes.response || {
            confirmationNumber,
            regionalOffice,
          };
          onDone(response);
        } else {
          // needs to start with this string to get the right message on the form
          throw new Error(
            `vets_server_error_burial: status ${res.data.attributes.state}`,
          );
        }
      })
      .catch(onError);
  }, window.VetsGov.pollTimeout || POLLING_INTERVAL);
}

function transformCountryCode(countryCode) {
  switch (countryCode) {
    case 'USA':
      return 'US';
    case 'MEX':
      return 'MX';
    case 'CAN':
      return 'CA';
    default:
      return countryCode;
  }
}

export function transform(formConfig, form) {
  const correctedForm = {
    ...form,
    data: {
      ...form?.data,
      claimantAddress: {
        ...form?.data?.claimantAddress,
        country: transformCountryCode(form?.data?.claimantAddress?.country),
      },
    },
  };
  const formData = transformForSubmit(formConfig, correctedForm);
  return JSON.stringify({
    burialClaim: {
      // this is ugly but the address schema helper within the forms system coerces all country codes back to alpha 3
      form: formData,
    },
    // can’t use toISOString because we need the offset
    localTime: moment().format('Y-MM-DD[T]kk:mm:ssZZ'),
  });
}

export function submit(form, formConfig) {
  const headers = { 'Content-Type': 'application/json' };

  const body = transform(formConfig, form);
  const apiRequestOptions = {
    headers,
    body,
    method: 'POST',
    mode: 'cors',
  };

  const onSuccess = resp => {
    const { guid, confirmationNumber, regionalOffice } = resp.data.attributes;
    return new Promise((resolve, reject) => {
      pollStatus(
        { guid, confirmationNumber, regionalOffice },
        response => {
          window.dataLayer.push({
            event: `${formConfig.trackingPrefix}-submission-successful`,
          });
          return resolve(response);
        },
        error => reject(error),
      );
    });
  };

  const onFailure = respOrError => {
    if (respOrError instanceof Response && respOrError.status === 429) {
      const error = new Error('vets_throttled_error_burial');
      error.extra = parseInt(respOrError.headers.get('x-ratelimit-reset'), 10);

      return Promise.reject(error);
    }
    return Promise.reject(respOrError);
  };

  return apiRequest('/burial_claims', apiRequestOptions)
    .then(onSuccess)
    .catch(onFailure);
}
export const serviceRecordNotification = (
  <div className="usa-alert usa-alert-warning background-color-only">
    <span>
      <strong>Note:</strong> If you would rather upload a DD214 than enter dates
      here, you can do that later in the form.
    </span>
  </div>
);

export const serviceRecordWarning = (
  <div className="usa-alert usa-alert-warning background-color-only">
    <span>
      <strong>Note:</strong> If you chose to upload a DD214 instead of recording
      service periods, you can do that here
    </span>
  </div>
);

export const transportationWarning = (
  <div
    className="usa-alert usa-alert-warning background-color-only"
    aria-live="polite"
  >
    <span>
      <strong>Note:</strong> At the end of the application, you will be asked to
      upload documentation for the expenses you incurred for transporting the
      Veteran’s remains.
    </span>
  </div>
);

export const BurialDateWarning = () => {
  const [shouldRender, setShouldRender] = useState(false);

  useEffect(() => {
    // NOTE: If we don't wait at least 900ms to render,
    // the alert content gets overspoken by the year content
    // when using a Screen Reader.
    // Using 1000ms to give a bit of padding
    const timeout = setTimeout(() => setShouldRender(true), 1000);
    return () => clearTimeout(timeout);
  }, []);

  return (
    <div aria-live="polite">
      {shouldRender && (
        <va-alert background-only show-icon status="warning">
{'fraction_non_alphanumeric': 0.12205729412876998, 'fraction_numerical': 0.005578141249881819, 'mean_word_length': 2.7523944661227384, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2121173', 'n_tokens_mistral': 3096, 'n_tokens_neox': 3004, 'n_words': 839}