import type { key_val } from '$lib/stores/ae_stores';
import { get_object } from './api_get_object';

// Updated 2023-12-01
export async function get_ae_obj_id_crud({
	api_cfg,
	no_account_id = false,
	obj_type,
	obj_id,
	use_alt_table = false,
	use_alt_base = false,
	inc = {},
	enabled = 'enabled',
	hidden = 'not_hidden',
	limit = 999999,
	offset = 0,
	data = {},
	// key,
	// jwt = null,
	headers = {},
	params = {},
	timeout = 25000,
	return_meta = false,
	log_lvl = 0
}: {
	api_cfg: any;
	no_account_id?: boolean;
	obj_type: string;
	obj_id: string;
	use_alt_table?: boolean;
	use_alt_base?: boolean;
	inc?: any;
	enabled?: 'enabled' | 'all' | 'not_enabled' | undefined;
	hidden?: 'hidden' | 'all' | 'not_hidden' | undefined;
	limit?: number;
	offset?: number;
	data?: any;
	// key: string,
	// jwt?: string,
	headers?: any;
	params?: key_val;
	timeout?: number;
	return_meta?: boolean;
	log_lvl?: number;
}) {
	if (log_lvl) {
		console.log('*** get_ae_obj_id_crud() ***');
	}

	// data = {};
	// data['super_key'] = key;
	// data['jwt'] = jwt;
	// NOTE: The key and or JWT should be in the header of the DELETE, GET, PATCH, POST

	let endpoint = '';
	if (obj_type == 'account') {
		endpoint = `/crud/account/${obj_id}`;
	} else if (obj_type == 'address') {
		endpoint = `/crud/address/${obj_id}`;
	} else if (obj_type == 'archive') {
		endpoint = `/crud/archive/${obj_id}`;
	} else if (obj_type == 'archive_content') {
		endpoint = `/crud/archive/content/${obj_id}`;
	} else if (obj_type == 'contact') {
		endpoint = `/crud/contact/${obj_id}`;
	} else if (obj_type == 'data_store') {
		endpoint = `/crud/data_store/${obj_id}`;
	} else if (obj_type == 'event') {
		endpoint = `/crud/event/${obj_id}`;
	} else if (obj_type == 'event_abstract') {
		endpoint = `/crud/event/abstract/${obj_id}`;
	} else if (obj_type == 'event_badge') {
		endpoint = `/crud/event/badge/${obj_id}`;
	} else if (obj_type == 'event_device') {
		endpoint = `/crud/event/device/${obj_id}`;
	} else if (obj_type == 'event_exhibit') {
		endpoint = `/crud/event/exhibit/${obj_id}`;
	} else if (obj_type == 'event_exhibit_tracking') {
		endpoint = `/crud/event/exhibit/tracking/${obj_id}`;
	} else if (obj_type == 'event_file') {
		endpoint = `/crud/event/file/${obj_id}`;
	} else if (obj_type == 'event_location') {
		endpoint = `/crud/event/location/${obj_id}`;
	} else if (obj_type == 'event_person') {
		endpoint = `/crud/event/person/${obj_id}`;
	} else if (obj_type == 'event_presentation') {
		endpoint = `/crud/event/presentation/${obj_id}`;
	} else if (obj_type == 'event_presenter') {
		endpoint = `/crud/event/presenter/${obj_id}`;
	} else if (obj_type == 'event_session') {
		endpoint = `/crud/event/session/${obj_id}`;
	} else if (obj_type == 'event_track') {
		endpoint = `/crud/event/track/${obj_id}`;
	} else if (obj_type == 'grant') {
		endpoint = `/crud/grant/${obj_id}`;
	} else if (obj_type == 'hosted_file') {
		endpoint = `/crud/hosted_file/${obj_id}`;
	} else if (obj_type == 'journal') {
		endpoint = `/crud/journal/${obj_id}`;
	} else if (obj_type == 'journal_entry') {
		endpoint = `/crud/journal/entry/${obj_id}`;
	} else if (obj_type == 'order') {
		endpoint = `/crud/order/${obj_id}`;
	} else if (obj_type == 'order_line') {
		endpoint = `/crud/order/line/${obj_id}`;
	} else if (obj_type == 'page') {
		endpoint = `/crud/page/${obj_id}`;
	} else if (obj_type == 'person') {
		endpoint = `/crud/person/${obj_id}`;
	} else if (obj_type == 'post') {
		endpoint = `/crud/post/${obj_id}`;
	} else if (obj_type == 'post_comment') {
		endpoint = `/crud/post/comment/${obj_id}`;
	} else if (obj_type == 'site') {
		endpoint = `/crud/site/${obj_id}`;
	} else if (obj_type == 'site_domain') {
		endpoint = `/crud/site/domain/${obj_id}`;
	} else if (obj_type == 'sponsorship_cfg') {
		endpoint = `/crud/sponsorship/cfg/${obj_id}`;
	} else if (obj_type == 'sponsorship') {
		endpoint = `/crud/sponsorship/${obj_id}`;
		// } else if (obj_type == 'user') {
		//     endpoint = `/crud/user/${obj_id}`;
	} else {
		console.log(`Unknown object type: ${obj_type}`);
		return false;
	}
	if (log_lvl) {
		console.log('Endpoint:', endpoint);
	}

	params['use_alt_table'] = use_alt_table;
	params['use_alt_base'] = use_alt_base;

	if (log_lvl) {
		console.log('Params:', params);
	}

	if (no_account_id) {
		headers['x-no-account-id'] = 'Nothing to See Here';
		delete headers['x-account-id'];
		delete api_cfg['headers']['x-account-id'];
		// headers['x-account-id'] = null;
		// headers['x-account-id'] = '_XY7DXtc9Mxx';
		// params['x_no_account_id_token'] = 'Nothing to See Here';

		// Remove the x-account-id header
		// if (headers['x-account-id']) {
		//     delete headers['x-account-id'];
		// }

		// headers['x-account-id'] = null;
		// headers['x-no-account-id-token'] = 'Nothing to See Here'; // get_object() will fix the underscores to dashes
	}

	const object_obj_get_promise = await get_object({
		api_cfg: api_cfg,
		endpoint: endpoint,
		headers: headers,
		params: params,
		timeout: timeout,
		log_lvl: log_lvl
	}).catch(function (error: any) {
		console.log('API GET CRUD object ID request failed.', error);
	});

	if (log_lvl > 1) {
		console.log('GET Object result =', object_obj_get_promise);
	}

	return object_obj_get_promise;
}
