<?php

namespace Drupal\acquia_dam\Hook;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\image\ImageStyleInterface;

/**
 * OOP hooks for image style operations.
 */
final class ImageStyleHooks {

  public function __construct(
    private readonly ConfigFactoryInterface $configFactory
  ) {}


  /**
   * Implements hook_image_style_delete().
   *
   * @param \Drupal\image\ImageStyleInterface $image_style
   *   The image style.
   */
  #[Hook('image_style_delete')]
  public function imageStyleDelete(ImageStyleInterface $image_style): void {
    // DAM-specific cleanup only (core handles cache tags).
    $config = $this->configFactory->getEditable('acquia_dam.settings');
    $allowed_styles = $config->get('allowed_image_styles') ?? [];
    if (($key = array_search($image_style->id(), $allowed_styles)) !== FALSE) {
      unset($allowed_styles[$key]);
      $config->set('allowed_image_styles', array_values($allowed_styles))->save();
    }
  }
}
