<?php

namespace Blazemedia\Affiliation\AdminUserInterface\Buttons;

use Blazemedia\Affiliation\API\CachedApi;

/*
 *  Class AffiliateButton ( nuovo )
 *  
 *  Bottone e UI per la creazione degli shortcode dei link
 */
class AffiliateButton extends Button {

    function __construct() { 
        
        /** 
         * Set the name of the plugin 
         * >> affiliation_button.js
         * 
         * and the name of the ajax endpoint to call into plugin
         * >> affiliation_button_action
         */
        parent::__construct( 'affiliation_button', 'affiliation_button_action' , 'dialog-AffiliateButton.html');       
    }


    public function addFieldsToDialog() : array { 

        $formats = json_decode( CachedApi::getInstance()->get(BLZ_API_FORMAT_PROVIDER.'buttons/'), true );

        $formats = array_filter( $formats, function( $format ) { return $format['code'] == 'btn'; });

        return [
            [
                'name'=> 'format_id',
                'value' => empty($formats) ? '0' : array_shift($formats)['id']
            ],[
                'name'  => 'home_url',
                'value' => get_home_url()
            ],
        ];
    }

    
}