<?php
// @Dion - why is this abstracted? It's only used by StorySourceOriginalDateFieldEntity.
// Why not just use php date formats there?
declare(strict_types=1);

namespace AuxCF\Entities\Field\Enum;

use AuxCF\Entities\Settings\Traits\SettingEnumTraits;

enum DateFieldFormatEnum: string
{
  use SettingEnumTraits;

  case ISO8601 = 'c';       // For metatags https://ogp.me/#datetime
  case MDY = 'm/d/Y';
  case Full = 'F j, Y';
  case FullAbbr = 'M j, Y';
  case ApAbbr = 'M. j, Y'; // AP Style

  public const SETTING_KEY = 'format';
  public const SETTING_LABEL = 'field format';

  public const DEFAULT = self::MDY;

  public function formatDate(\DateTime|string $date): string
  {
    $formatString = $this->value;

    if (is_string($date)) {
      $date = new \DateTime($date);
    }

    if (self::ApAbbr === $this) {
      $monthNumber = (int) $date->format('n');

      if (!in_array($monthNumber, [1, 2, 8, 9, 10, 11, 12])) {
        $formatString = 'F j, Y';
      }
    }

    return $date->format($formatString);
  }
}
