<?php
namespace Dreamfox\DeliveryDate\Abstracts;

use Dreamfox\DeliveryDate\Helpers\CartHelper;
use Dreamfox\DeliveryDate\Helpers\DeliveryDateHelper;

abstract class AbstractCheckoutFieldManager
{

  protected $_fields;

  public function __construct($fields)
  {
    $this->_fields = $fields;
  }

  abstract public function process();

  public function getFields()
  {
    return $this->_fields;
  }

  protected function _isAvailable($appliedCategories)
  {
    $cartCategories = CartHelper::getCategories();
    $hasAtLeastOne = !empty(array_intersect($cartCategories, $appliedCategories));
    return $hasAtLeastOne;
  }

  /**
   * @param int   $datesToDeliver       Minimum lead time in days.
   * @param array $minDateOptions       Optional keys: countOnlyWorkingDays, disabledWeekdays, holidayDatesYmd.
   */
  protected function _getMinDeliveryDate($datesToDeliver, array $minDateOptions = array())
  {
    $countOnlyWorkingDays = ! empty( $minDateOptions['countOnlyWorkingDays'] );
    $disabledWeekdays     = isset( $minDateOptions['disabledWeekdays'] ) ? $minDateOptions['disabledWeekdays'] : array();
    $holidayDatesYmd      = isset( $minDateOptions['holidayDatesYmd'] ) ? $minDateOptions['holidayDatesYmd'] : array();

    $timestamp = DeliveryDateHelper::calculateMinTimestamp(
      $datesToDeliver,
      $countOnlyWorkingDays,
      $disabledWeekdays,
      $holidayDatesYmd
    );

    return DeliveryDateHelper::formatMinDeliveryDate( $timestamp );
  }
}
