import axios, { Axios } from "axios";

import { User } from "./User";
import { APIToken } from './APIToken';

class ClientCreateOptions {
    /** Key used to access APIs, if you don't have user data
     * @type {APIToken?}
     */
    key;

    /** Current user's data
     * @type {User?}
     */
    user;
}

/**
 * An API client which is basically a User but with
 * API token and with access to private fields.
 */
class Client extends User {
    /** @type {APIToken} */
    key;

    /** @type {string} */
    openid_id;

    /** @type {string} */
    favorites_apartments;

    /** @type {string} */
    comparison_apartments;

    /**
     * 
     * @param {ClientCreateOptions} options 
     */
    constructor(options) {
        super();

        if (options.key === undefined & options.user === undefined) {
            throw new Error('Either key or user is required.');
        }

        this.key = options.key;
        if (options.user) {
            for (const key in options.user) {
                this[key] = options.user[key];
            }
        }
    }

    /**
     * 
     * @param {string} uri 
     * @param {string} method 
     * @param {import('axios').AxiosRequestConfig<any>} options 
     */
    fetchData(url, method, options) {
        axios.request({
            url,
            method,
            headers: {
                ...(options.headers ? options.headers : {}),
                'Authorization': this.key.key
            },

            ...options
        });
    }
}