class PrimeVideoLink {

    constructor( url, proxy = `/wp-json/api/v1/proxy/` ) {

        this.url = url;

        this.platform = 'Primevideo';
        this.proxy = proxy;

        //this.asin = this.getAsin();
    }


    isValid() {
        return this.asin != null;
    }

    isLink() {

        return this.url.includes('primevideo')
    }

    addTag( tag ) {

        if( this.url.includes('tag=') ) return this.url.replace(/tag=.*?(&|$)/, `tag=${tag}$1`);

        return this.url.includes('?') ? `${this.url}&tag=${tag}` : `${this.url}?tag=${tag}`;
    } 

    
/*     getAsin() {

        this.url = this.url.replace('gp/product', 'dp');

        /// se non trova nulla ritorna la url tale e quale        
        let asin = this.url.replace(/.*dp\/(.*?)(\/.*|$|\?.*)/ , '$1' );
        
        this.asin = asin === this.url ? null : asin;

        return this.asin;
    }
 */

    /**
     * effettua la chiamata e prende i dati del link     
     */
    async getData( api_call ) {

        let response = await fetch( `${this.proxy}${api_call}` )
        this.data = await response.json();
        
        return this.data;
    }
    

}