<?php

/**
 * @file
 * Install, update, and uninstall functions for the Checklist API module.
 */

use Drupal\checklistapi\ChecklistapiChecklist;
use Drupal\Core\Render\Element;

/**
 * Update saved progress configuration to new schema.
 */
function checklistapi_update_8001() {
  $definitions = checklistapi_get_checklist_info();
  foreach ($definitions as $id => $definition) {
    $checklist = checklistapi_checklist_load($id);

    /**
     * Avoid trying to operate on null due to failure to load a checklist.
     * @see https://www.drupal.org/project/checklistapi/issues/3122093
     */
    if (!$checklist->config) {
      continue;
    }

    $config = $checklist->config->get(ChecklistapiChecklist::PROGRESS_CONFIG_KEY);
    $config['#items'] = [];

    $groups = $checklist->items;
    foreach (Element::children($groups) as $group_key) {
      $group = $groups[$group_key];
      // Loop through items.
      foreach (Element::children($group) as $item_key) {
        if ($config[$item_key]) {
          $config['#items'][$item_key] = $config[$item_key];
        }
        unset($config[$item_key]);
      }
    }

    $checklist->config->set(ChecklistapiChecklist::PROGRESS_CONFIG_KEY, $config)
      ->save();
  }

}
