import { ALWAYSAI_DO_NOT_TRACK } from '../environment';
import { CliAuthenticationClient } from './authentication-client';

export async function postTrackingDataToHubspot(
  property: string,
  value: string | boolean
) {
  if (ALWAYSAI_DO_NOT_TRACK) {
    return;
  }

  let email = 'undefined';

  try {
    ({ email } = await CliAuthenticationClient({ readonly: true }).getInfo());
  } catch (e) {
    return;
  }

  const data = {
    properties: [
      {
        property,
        value
      }
    ]
  };

  const body = {
    data,
    email
  };

  try {
    await fetch(
      'https://ausfl8paoe.execute-api.us-west-2.amazonaws.com/dev2/post-survey-responses',
      {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(body),
        redirect: 'follow'
      }
    );
  } catch (e) {
    return;
  }
}
