<?php

/**
 * @file
 * page-attachments.php
 */

use Drupal\Core\Render\Markup;

/**
 * Implements hook_page_attachments_alter().
 */
function naomi_2025_page_attachments_alter(array &$page) {
  $theme_path = \Drupal::service('extension.list.theme')->getPath('naomi_2025');

  $critical_css_files = [
    'base/base.css',
    'block/branding-block/block.branding-block.css',
    'site-layout/site-header/site-header.css',
    'site-layout/site-mode-switcher/site-mode-switcher.css',
    'site-layout/menu-toggle/menu-toggle.css',
    'site-layout/site-main/site-main.css',
    'base/data-site-mode/at-work.css',
    'base/data-site-mode/at-play.css',
  ];

  if (!empty($critical_css_files)) {
    foreach ($critical_css_files as $css_file) {
      $css = file_get_contents("$theme_path/ui/dist/$css_file");

      $page['#attached']['html_head'][] = [
        [
          '#tag' => 'style',
          '#value' => Markup::create($css),
        ],
        "naomi_2025.{$css_file}",
      ];
    }
  }
}
