import { Client } from "./client.js";

class Profile
{
	/**
	 * @param  {Client} client
	 */
	constructor (client, username, id = undefined, avatar_url = undefined)
	{
		/**
		 * @type  {Client} client
		 */
		this.client = client;
		
		/**
		 * @type {String}
		 */
		this.username = username;

		/**
		 * @type {Number}
		 */
		this.id = id;

		/**
		 * @type {String}
		 */
		this.avatar_url = avatar_url;

		/**
		 * @type {Boolean}
		 */
		this.isBlocked = false;
	}

	/**
	 * 
	 * @returns {Promise<*>}
	 */
	async init()
	{
		let response = await this.client._get(`/api/profiles/${this.username}`);

		if (response.status !== 200)
			return response.status;

		let response_data = await response.json();
		this.id = response_data.user_id;
		this.username = response_data.username;
		this.avatar_url = response_data.avatar_url;

		if (this.client.me == undefined)
			return;

		let block_response = await this.client._get("/api/profiles/block");

		if (block_response.status != 200)
			return

		let block_data = await block_response.json();
		let block_list = JSON.parse(block_data);
		block_list.forEach(block => {
		 	let blocker = block.fields.blocker;
		 	let blocked = block.fields.blocked;
		 	if (blocker == this.client.me.user_id && blocked == user_id)
		 		return this.isBlocked = true;
		});
	}
}

export {Profile}
