<?php
namespace Dreamfox\DeliveryDate\Views;

use Dreamfox\DeliveryDate\Facades\SystemFacade;
use Dreamfox\DeliveryDate\Helpers\DateHelper;
use Dreamfox\DeliveryDate\Helpers\DeliveryDateHelper;
use Dreamfox\DeliveryDate\Interfaces\ApplicationInterface;

class DeliveryDateView extends BaseView
{

  public function __construct(ApplicationInterface $app)
  {
    parent::__construct($app);
    $this->setTemplate('delivery_date');
  }

	public function getMinDate()
	{
		$deliveryDays = SystemFacade::getInstance()->loadSettings()->getDeliveryDays();
		$timestamp    = DeliveryDateHelper::calculateMinTimestamp( $deliveryDays, false, array(), array() );
		return DeliveryDateHelper::toJsMinDate( $timestamp );
	}

	/**
	 * Whether timeslots are enabled in settings.
	 */
	public function isTimeslotsEnabled()
	{
		return SystemFacade::getInstance()->loadTimeslotSettings()->getEnabled();
	}

	/**
	 * Data for the JS layer — only the keys the checkout script needs.
	 */
	public function getTimeslotData()
	{
		$settings = SystemFacade::getInstance()->loadTimeslotSettings();
		return array(
			'enabled'   => $settings->getEnabled(),
			'ajaxUrl'   => admin_url( 'admin-ajax.php' ),
			'nonce'     => wp_create_nonce( 'dreamfox_wdd_frontend' ),
			'i18n'      => array(
				'loading'    => __( 'Loading timeslots…', 'woocommerce-delivery-date' ),
				'selectSlot' => __( '— Select a timeslot —', 'woocommerce-delivery-date' ),
				'noSlots'    => __( 'No timeslots available for this date.', 'woocommerce-delivery-date' ),
				'selectDate' => __( '— Please select a delivery date first —', 'woocommerce-delivery-date' ),
			),
		);
	}
}
