import { OauthConnections } from '@event-inc/types';

interface OauthConnectionOptions {
	OAUTH_APP_CLIENT_ID: string;
	OAUTH_APP_CLIENT_SECRET: string;
	type: OauthConnections;
}

enum OauthConnectionTypes {
	Xero = 'xero',
}

const endpoints = {
	xero: 'https://identity.xero.com/connect/token',
};

export class OauthConnection {
	private readonly OAUTH_APP_CLIENT_ID: string;
	private readonly OAUTH_APP_CLIENT_SECRET: string;
	private readonly type: string;

	constructor({
		OAUTH_APP_CLIENT_ID,
		OAUTH_APP_CLIENT_SECRET,
		type,
	}: OauthConnectionOptions) {
		this.OAUTH_APP_CLIENT_ID = OAUTH_APP_CLIENT_ID;
		this.OAUTH_APP_CLIENT_SECRET = OAUTH_APP_CLIENT_SECRET;
		this.type = type;
	}

	getOauthDetails = () => {
		switch (this.type) {
			case OauthConnectionTypes.Xero:
				return {
					endpoint: endpoints[this.type],
					headers: {
						'Content-Type': 'application/x-www-form-urlencoded',
						Authorization: `Basic ${Buffer.from(
							`${this.OAUTH_APP_CLIENT_ID}:${this.OAUTH_APP_CLIENT_SECRET}`
						).toString('base64')}`,
					},
					sourceIntegrationDefinitionId: 'conn_def_src_MTY4ODc2MzM3ODY2OQ::OWE2NDdmYjAtZjFlMy00MWEyLTgwMTctMzVhODMxYTBkOWNi',
					destinationIntegrationDefinitionId:
						'conn_def_dst_MTY4NjA3MDUyOTk1OA::MTgwM2E1NGItMTYxMC00NjE4LTk1NTItNjBjY2I1NmY1ZmQx',
				};
		}
	};

	getOauthFormData = ({
		accessToken,
		refreshToken,
	}: {
		accessToken: string;
		refreshToken: string;
	}) => {
		switch (this.type) {
			case OauthConnectionTypes.Xero:
				return {
					XERO_REFRESH_TOKEN: refreshToken,
					XERO_ACCESS_TOKEN: accessToken,
					XERO_CLIENT_ID: this.OAUTH_APP_CLIENT_ID,
					XERO_CLIENT_SECRET: this.OAUTH_APP_CLIENT_SECRET,
				};
		}
	};
}

