import { Profile } from "./profile.js";

class Profiles
{
	/**
	 * @param  {Client} client
	 */
	constructor (client)
	{
		/**
		 * @type {Client} client
		 */
		this.client = client
	}

	/**
	 * 
	 * @returns {Promise<[Profile]>}
	 */
	async all()
	{
		let response = await this.client._get("/api/profiles/");
		let response_data = await response.json();

		let profiles = []
		response_data.forEach((profile) => {
			profiles.push(new Profile(this.client, profile.username, profile.user_id, profile.avatar_url))
		});
		return profiles;
	}

	/**
	 * 
	 * @param {String} username 
	 * @returns {?Profile}
	 */
	async getProfile(username)
	{
		let profile = new Profile(this.client, username);
		if (await profile.init())
			return null;
		return profile;
	}

	/**
	 * Block a user
	 * @param {Number} user_id 
	 * @returns {Promise<Object>}
	 */
	async block(user_id) {

		// blocker & blocked
		let response = await this.client._post("/api/profiles/block", { 
			users_id:[this.client.me.user_id, user_id],
		});

		let data = await response.json();
		return data;

	}

	/**
	 * Unblock a user
	 * @param {Number} user_id 
	 * @returns {Promise<Object>}
	 */
	async deblock(user_id) {

		// blocker & blocked
		let response = await this.client._delete("/api/profiles/block", { 
			users_id:[this.client.me.user_id, user_id],
		});

		let data = await response.json();
		return data;
		
	}
}

export {Profiles}
