export const activateLicense = async (licenseKey: string): Promise<Response> => {
    const response = await fetch(CouponsPlusWelcome.urls.API.activate, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-WP-Nonce': CouponsPlusWelcome.security.nonces.wp,
        },
        body: JSON.stringify({
            license_key: licenseKey
        })
    })
    if (!response.ok) {
        throw new Error('Failed to activate license. code: '  + response.statusText + '. body: ' + await response.text());
    }
    return response
}
export const deactivateLicense = async (): Promise<Response> => {
    const response = await fetch(CouponsPlusWelcome.urls.API.deactivate, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'X-WP-Nonce': CouponsPlusWelcome.security.nonces.wp,
        },
        body: JSON.stringify({
        })
    })
    if (!response.ok) {
        throw new Error('Failed to deactivate license. Please refresh the page and try again. code: '  + response.statusText + '. body: ' + await response.text());
    }
    return response
}

export type LicenseData = {
    "is_activated": boolean,
    "data": {
        "activated_at": string,
        "status": string,
        "expires_at": string,
        "key": string
    }
};
export const loadLicenseData = async (): Promise<LicenseData> => {
    const response = await fetch(CouponsPlusWelcome.urls.API.status, {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
            'X-WP-Nonce': CouponsPlusWelcome.security.nonces.wp,
        }
    })

    if (!response.ok) {
        throw new Error('Failed to load license data. code: '  + response.statusText + '. body: ' + await response.text());
    }

    return response.json()
}