<?php

namespace Blazemedia\Affiliation\AdminUserInterface\Buttons;

/*
 *  Class AffiliateZone
 *  
 *  Bottone e UI per la creazione degli shortcode delle zone
 */
class AffiliateZone extends Button {

    function __construct() { 
        
        /** 
         * Set the name of the plugin 
         * >> affiliation_link.js
         * 
         * and the name of the ajax endpoint to call into plugin
         * >> affiliation_link_action
         */
        parent::__construct( 'affiliation_zone', 'affiliation_zone_action', 'dialog-AffiliateZone.html' );
    }

    public function addFieldsToDialog() : array { 
        
        return [

            [
                'name' => 'Zones',
                'value' => json_encode( $this->getZones() )
            ], 

        ];
    }


    protected function getZones() : array {

        $zones = get_posts(['post_type'=>'affiliate_zone', 'posts_per_page' => -1 ]);

        if ( empty( $zones ) ) return [];

        return array_map( fn( $zone) => [
            'id'   => $zone->ID,
            'name' => $zone->post_title            
        ], $zones ) ;

        
    }

}