<?php

use Drupal\Core\Site\Settings;

/**
 * Update configs.
 */
function kamihaya_cms_common_update_10001() {
  _kamihaya_cms_common_change_configs('kamihaya_cms_common', [
    'core.date_format.kamihaya_date_time.yml',
    'core.date_format.kamihaya_date.yml',
    'core.date_format.kamihaya_year_month.yml',
  ], 'install');

}

/**
 * Update configs.
 */
function kamihaya_cms_common_update_10002() {
  _kamihaya_cms_common_change_configs('kamihaya_cms_common', [
    'editor.editor.full_html.yml',
    'filter.format.full_html.yml',
  ], 'optional');
}

/**
 * Update configs.
 */
function kamihaya_cms_common_update_10003() {
  $editor_config = \Drupal::configFactory()->getEditable('editor.editor.full_html');
  $file_config = _kamihaya_cms_common_load_config_file('kamihaya_cms_common', 'editor.editor.full_html.yml', 'optional');
  if (!empty($editor_config) && !empty($file_config)) {
    $items = !empty($file_config['settings']['toolbar']['items']) ? $file_config['settings']['toolbar']['items'] : [];
    $plugins = !empty($file_config['settings']['plugins']) ? $file_config['settings']['plugins'] : [];
    if (!empty($items)) {
      $editor_config->set('settings.toolbar.items', _kamihaya_cms_common_merge_config($editor_config->get('settings.toolbar.items') ?: [], $items));
    }
    if (!empty($plugins)) {
      $editor_config->set('settings.plugins', _kamihaya_cms_common_merge_config($editor_config->get('settings.plugins') ?: [], $plugins));
    }
    $editor_config->save(TRUE);
  }

  $filter_config = \Drupal::configFactory()->getEditable('filter.format.full_html');
  $file_config = _kamihaya_cms_common_load_config_file('kamihaya_cms_common', 'filter.format.full_html.yml', 'optional');
  if (!empty($filter_config) && !empty($file_config)) {
    $dep_config = !empty($file_config['dependencies']['config']) ? $file_config['dependencies']['config'] : [];
    $dep_module = !empty($file_config['dependencies']['module']) ? $file_config['dependencies']['module'] : [];
    $filters = !empty($file_config['filters']) ? $file_config['filters'] : [];
    if (!empty($dep_config)) {
      $filter_config->set('dependencies.config', _kamihaya_cms_common_merge_config($filter_config->get('dependencies.config') ?: [], $dep_config));
    }
    if (!empty($dep_module)) {
      $filter_config->set('dependencies.module', _kamihaya_cms_common_merge_config($filter_config->get('dependencies.module') ?: [], $dep_module));
    }
    if (!empty($filters)) {
      $filter_config->set('filters', _kamihaya_cms_common_merge_config($filter_config->get('filters') ?: [], $filters));
    }
    $filter_config->save(TRUE);
  }

  if (!\Drupal::moduleHandler()->moduleExists('imce')) {
    return;
  }
  $admin_profile = \Drupal::configFactory()->getEditable('imce.profile.admin');
  if (empty($admin_profile)) {
    return;
  }
  $imce_config = \Drupal::configFactory()->getEditable('imce.settings');
  $imce_config->set('roles_profiles.administrator.public', 'admin');

  if (!empty(Settings::get('file_private_path'))) {
    $imce_config->set('roles_profiles.administrator.private', 'admin');
  }
  $imce_config->save(TRUE);
}

/**
 * Update full HTML edotor config.
 */
function kamihaya_cms_common_update_10004() {
  /** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
  $module_installer = \Drupal::service("module_installer");
  $modules = [];
  // Install CKEditor5 plugin pack modules.
  if (\Drupal::moduleHandler()->moduleExists('ckeditor5_plugin_pack')) {
    $modules[] = 'ckeditor5_plugin_pack';
  }
  if (\Drupal::moduleHandler()->moduleExists('ckeditor5_plugin_pack_font')) {
    $modules[] = 'ckeditor5_plugin_pack_font';
  }
  if (!empty($modules)) {
    $module_installer->install($modules);
  }

  // Update full HTML editor config.
  $editor_config = \Drupal::configFactory()->getEditable('editor.editor.full_html');
  $file_config = _kamihaya_cms_common_load_config_file('kamihaya_cms_common', 'editor.editor.full_html.yml', 'optional');
  if (!empty($editor_config) && !empty($file_config)) {
    $items = !empty($file_config['settings']['toolbar']['items']) ? $file_config['settings']['toolbar']['items'] : [];
    $plugins = !empty($file_config['settings']['plugins']) ? $file_config['settings']['plugins'] : [];
    if (!empty($items)) {
      $editor_config->set('settings.toolbar.items', _kamihaya_cms_common_merge_config($editor_config->get('settings.toolbar.items') ?: [], $items));
    }
    if (!empty($plugins)) {
      $editor_config->set('settings.plugins', _kamihaya_cms_common_merge_config($editor_config->get('settings.plugins') ?: [], $plugins));
    }
    $editor_config->save(TRUE);
  }

  // Uninstall CKEditor font plugin.
  if (\Drupal::moduleHandler()->moduleExists('ckeditor_font')) {
    $module_installer->uninstall(['ckeditor_font']);
  }
}

/**
 * Merge config array.
 *
 * @param array $base_config
 *   The base config.
 * @param array $new_config
 *   T¥he new config.
 *
 * @return array
 *   The merged config.
 */
function _kamihaya_cms_common_merge_config(array $base_config, array $new_config) {
  foreach ($new_config as $key => $value) {
    if ((is_string($key) && empty($base_config[$key]))
      || (is_int($key) && !in_array($value, $base_config))) {
      $base_config[$key] = $value;
    }
  }
  return $base_config;
}

/**
 * Enable remove_http_headers module.
 */
function kamihaya_cms_common_update_10005() {
  $installed = \Drupal::service('module_installer')->install(['remove_http_headers'], TRUE);
  if ($installed) {
    $config_factory = \Drupal::configFactory();
    $config = $config_factory->getEditable('remove_http_headers.settings');
    $config->set('headers_to_remove', ['X-Generator']);
    $config->save(TRUE);
  }
}

/**
 * Enable easy_breadcrumb module.
 */
function kamihaya_cms_common_update_10006() {
  $installed = \Drupal::service('module_installer')->install(['easy_breadcrumb'], TRUE);
  if ($installed) {
    $config_factory = \Drupal::configFactory();
    $config = $config_factory->getEditable('easy_breadcrumb.settings');
    $config->set('include_title_segment', TRUE);
    $config->set('capitalizator_mode', 'none');
    $config->save(TRUE);
  }
}
