import { getRequiredServerEnvVar } from '../utils/misc' ; const CONVERT_KIT_API_SECRET = getRequiredServerEnvVar ( 'CONVERT_KIT_API_SECRET' ) ; const CONVERT_KIT_API_KEY = getRequiredServerEnvVar ( 'CONVERT_KIT_API_KEY' ) ; type ConvertKitSubscriber = { id : number ; first_name : string ; email_address : string ; state : 'active' | 'inactive' ; created_at : string ; fields : Record < string , string | null > ; } ; type ConvertKitTag = { id : string ; name : string ; created_at : string ; } ; async function getConvertKitSubscriber ( email ) { const url = new URL ( 'https://api.convertkit.com/v3/subscribers' ) ; url . searchParams . set ( 'api_secret' , CONVERT_KIT_API_SECRET ) ; url . searchParams . set ( 'email_address' , email ) ; const resp = await fetch ( url . toString ( ) ) ; const json = await resp . json ( ) ; const { subscribers : [ subscriber = { state : 'inactive' } ] = [ ] } = json as { subscribers ? : Array < ConvertKitSubscriber > ; } ; return subscriber . state === 'active' ? subscriber : null ; } async function getConvertKitSubscriberTags ( subscriberId ) { const url = new URL ( `https://api.convertkit.com/v3/subscribers/${ subscriberId }/tags` ) ; url . searchParams . set ( 'api_secret' , CONVERT_KIT_API_SECRET ) ; const resp = await fetch ( url . toString ( ) ) ; const json = ( await resp . json ( ) ) as { tags : Array < ConvertKitTag > ; } ; return json . tags ; } async function ensureSubscriber ( { email , firstName , } ) { let subscriber = await getConvertKitSubscriber ( email ) ; if ( ! subscriber ) { subscriber = await addSubscriberToForm ( { email , firstName , convertKitFormId : '2500372' , } ) ; } return subscriber ; } async function addSubscriberToForm ( { email , firstName , convertKitFormId , } ) { const subscriberData = { api_key : CONVERT_KIT_API_KEY , api_secret : CONVERT_KIT_API_SECRET , first_name : firstName , email , } ; const response = await fetch ( `https://api.convertkit.com/v3/forms/${ convertKitFormId }/subscribe` , { method : 'post' , body : JSON . stringify ( subscriberData ) , headers : { 'Content-Type' : 'application/json' } , } ) ; const json = ( await response . json ( ) ) as { subscription : { subscriber : ConvertKitSubscriber ; } ; } ; return json . subscription . subscriber ; } async function addTagToSubscriber ( { email , firstName , convertKitTagId , } ) { await ensureSubscriber ( { email , firstName } ) ; const subscriberData = { api_key : CONVERT_KIT_API_KEY , api_secret : CONVERT_KIT_API_SECRET , first_name : firstName , email , } ; const subscribeUrl = `https://api.convertkit.com/v3/tags/${ convertKitTagId }/subscribe` ; const response = await fetch ( subscribeUrl , { method : 'post' , body : JSON . stringify ( subscriberData ) , headers : { 'Content-Type' : 'application/json' , } , } ) ; const json = ( await response . json ( ) ) as { subscription : { subscriber : ConvertKitSubscriber ; } ; } ; return json . subscription . subscriber ; } async function tagKCDSiteSubscriber ( { email , firstName , id , } ) { const subscriber = await getConvertKitSubscriber ( email ) ; const kcdTagId = '2466369' ; const kcdSiteForm = '2393887' ; const subscriberData = { api_key : CONVERT_KIT_API_KEY , api_secret : CONVERT_KIT_API_SECRET , first_name : firstName , email , fields : { kcd_site_id : id } , } ; const subscribeUrl = subscriber ? `https://api.convertkit.com/v3/tags/${ kcdTagId }/subscribe` : `https://api.convertkit.com/v3/forms/${ kcdSiteForm }/subscribe` ; const updatedRes = await fetch ( subscribeUrl , { method : 'post' , body : JSON . stringify ( subscriberData ) , headers : { 'Content-Type' : 'application/json' , } , } ) ; const updatedJson = ( await updatedRes . json ( ) ) as { subscription : { subscriber : ConvertKitSubscriber ; } ; } ; return updatedJson . subscription . subscriber ; } export { getConvertKitSubscriber , getConvertKitSubscriberTags , tagKCDSiteSubscriber , addTagToSubscriber , addSubscriberToForm , } ;