<?php

namespace Fab\Controller;

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

use Fab\Metabox\FABMetaboxSetting;
use Fab\Interfaces\JIT_Interface;

/**
 * FAB Module Link.
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Toast_Type_Frontend extends Toast_Type implements JIT_Interface {

    /**
     * Add toast data to FAB item.
     *
     * @param object $instance FAB item instance.
     * @return void
     */
    public function add_fab_item_data( $instance ) {
        if ( $instance->getType() !== $this->type ) {
            return;
        }

        // Get toast data.
        $default = FABMetaboxSetting::get_input()['fab_setting_toast']['default'];
        $toast   = get_post_meta( $instance->getID(), FABMetaboxSetting::$post_metas['toast']['meta_key'], true );
        $toast   = ( $toast ) ? \Fab\Plugin\Helper::getInstance()->ArrayMergeRecursive( (array) $default, (array) $toast ) : $default;
        $instance->setToast( $toast );

        // Set is button.
        $instance->setIsButton( false );
    }

    /*
    |--------------------------------------------------------------------------
    | Fulfill implemented interface contracts
    |--------------------------------------------------------------------------
     */

    /**
     * JIT Method.
     *
     * @return void
     */
    public static function jit() {
        add_action( 'fab_item_data', array( __CLASS__, 'jit_add_fab_item_data' ) );
    }
}
