import { makeRequest } from "@frontend/kitui";

import { parseAxiosError } from "@utils/parse-error";

const apiUrl = process.env.REACT_APP_DOMAIN + "/codeword/promocode";

export async function activatePromocode(promocode: string) {
  try {
    const response = await makeRequest<
      | {
          codeword: string;
        }
      | {
          fastLink: string;
        },
      {
        greetings: string;
      }
    >({
      url: apiUrl + "/activate",
      method: "POST",
      contentType: true,
      body: {
        codeword: promocode,
      },
    });

    return response.greetings;
  } catch (nativeError) {
    const [error] = parseAxiosError(nativeError);

    throw new Error(error);
  }
}
