<?php

namespace Fab\Controller;

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

use Fab\Interfaces\JIT_Interface;
use Fab\Helper\FABModal;

/**
 * Search Type Frontend
 *
 * @package    Fab
 * @subpackage Fab/Module
 */
class Search_Type_Frontend extends Search_Type implements JIT_Interface {

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

        // Set extra options.
        $label               = get_post_meta( $instance->getId(), $this->post_meta['label']['meta_key'], true );
        $pagination_enable   = get_post_meta( $instance->getId(), $this->post_meta['pagination_enable']['meta_key'], true );
        $pagination_per_page = get_post_meta( $instance->getId(), $this->post_meta['pagination_per_page']['meta_key'], true );
        $instance->setExtraOptions(
            array(
                'search' => array(
                    'label'               => $label ? $label : $this->post_meta['label']['default'],
                    'pagination_enable'   => $pagination_enable ? $pagination_enable : $this->post_meta['pagination_enable']['default'],
                    'pagination_per_page' => $pagination_per_page ? $pagination_per_page : $this->post_meta['pagination_per_page']['default'],
                ),
            )
        );

        $modal = new FABModal( $instance->getID() );
        $instance->setModal( $modal );
    }

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

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