<?php

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

/**
 * Install token module.
 */
function activity_basics_update_8101(array &$sandbox) : void {

  $modules = [
    'token',
  ];
  \Drupal::service('module_installer')->install($modules);

}

/**
 * Install activity_send_email module.
 */
function activity_basics_update_8102() : void {

  $modules = [
    'activity_send_email',
  ];
  \Drupal::service('module_installer')->install($modules);

}

/**
 * Replace message template activity_bundle_entity single value with multiple.
 */
function activity_basics_update_8103() : void {

  $message_templates = [
    'create_comment_community_node',
    'create_comment_community_post',
    'create_comment_group_node',
    'create_comment_group_post',
    'create_event_community',
    'create_event_group',
    'create_topic_community',
    'create_topic_group',
    'create_post_community',
    'create_post_group',
    'create_post_profile',
  ];

  foreach ($message_templates as $message_template) {
    $config_name = "message.template.{$message_template}";
    $config = \Drupal::service('config.factory')->getEditable($config_name);

    $activity_bundle_entity = $config->get('third_party_settings.activity_logger.activity_bundle_entity');
    $config->clear('third_party_settings.activity_logger.activity_bundle_entity');

    $bundle_entity = explode('.', $activity_bundle_entity ?? "");
    $activity_bundle_entity_new = $bundle_entity[0] . '-' . $bundle_entity[1];
    $activity_bundle_entities = [$activity_bundle_entity_new => $activity_bundle_entity_new];

    $config->set('third_party_settings.activity_logger.activity_bundle_entities', $activity_bundle_entities);
    $config->save();
  }

}
