<?php

use Drupal\Component\Serialization\Yaml;

/**
 * Update configs.
 */
function kamihaya_cms_media_type_update_10001() {
  _kamihaya_cms_common_change_configs('kamihaya_cms_media_type', [
    'media.type.video.yml',
    'language.content_settings.media.video.yml',
    'field.storage.media.field_thumbnail_image.yml',
    'field.storage.media.field_media_video_file.yml',
    'field.field.media.document.field_thumbnail_image.yml',
    'field.field.media.video.field_media_video_file.yml',
    'core.entity_view_mode.media.document_list.yml',
    'core.entity_form_display.media.video.default.yml',
    'core.entity_form_display.media.video.media_library.yml',
    'core.entity_view_display.media.video.default.yml',
    'core.entity_view_display.media.video.media_library.yml',
    'core.entity_form_display.media.document.default.yml',
    'core.entity_form_display.media.document.media_library.yml',
    'core.entity_view_display.media.document.default.yml',
    'core.entity_view_display.media.document.document_list.yml',
    'core.entity_view_display.media.document.media_library.yml',
    'views.view.document_list.yml',
  ], 'install');

  _kamihaya_cms_common_save_translation_configs([
    'media.type.video.yml',
    'field.storage.media.field_thumbnail_image.yml',
    'field.storage.media.field_media_video_file.yml',
    'field.field.media.default.field_media_video_file.yml',
    'views.view.document_list.yml',
  ], 'ja');
}

/**
 * Update configs.
 */
function kamihaya_cms_media_type_update_10002() {
  _kamihaya_cms_common_change_configs('kamihaya_cms_media_type' , [
    'taxonomy.vocabulary.document_type.yml',
    'language.content_settings.taxonomy_term.document_type.yml',
    'core.entity_form_display.taxonomy_term.document_type.default.yml',
    'views.view.document_list.yml',
  ], 'install');

  _kamihaya_cms_common_save_translation_configs([
    'taxonomy.vocabulary.document_type.yml',
    'views.view.document_list.yml',
  ], 'ja');
}

/**
 * Update configs.
 */
function kamihaya_cms_media_type_update_10003() {
  _kamihaya_cms_common_change_configs('kamihaya_cms_media_type' , [
    'field.storage.media.field_document_type.yml',
    'field.field.media.document.field_document_type.yml',
    'core.entity_form_display.media.document.default.yml',
    'core.entity_form_display.media.document.media_library.yml',
    'core.entity_view_display.media.document.default.yml',
    'core.entity_view_display.media.document.document_list.yml',
    'core.entity_view_display.media.document.media_library.yml',
    'views.view.document_list.yml',
  ], 'install');

  _kamihaya_cms_common_save_translation_configs([
    'field.field.media.document.field_document_type.yml',
  ], 'ja');
}

/**
 * Update configs.
 */
function kamihaya_cms_media_type_update_10004() {
  $fields = [
    'field.field.media.document.field_thumbnail_image',
    'field.field.media.image.field_media_image',
  ];
  foreach($fields as $field) {
    if ($field_config = \Drupal::configFactory()->getEditable($field)) {
      $value = explode(' ', $field_config->get('settings.file_extensions'));
      if (!in_array('svg', $value)) {
        $value[] = 'svg';
      }
      if (!in_array('webp', $value)) {
        $value[] = 'webp';
      }
      $field_config->set('settings.file_extensions', implode(' ', $value));
      $field_config->save(TRUE);
    }
  }
}

/**
 * Add convert to webp image style.
 */
function kamihaya_cms_media_type_update_10005() {
  $image_style = \Drupal::entityTypeManager()->getStorage('image_style')->load('convert_to_webp');
  if ($image_style) {
    return;
  }

  _kamihaya_cms_common_change_configs('kamihaya_cms_media_type' , [
    'image.style.convert_to_webp.yml',
  ], 'install');
}

/**
 * Implements hook_update_dependencies().
 */
function kamihaya_cms_media_type_update_dependencies() {
  $dependencies['kamihaya_cms_media_type'] = [
    10005 => [
      'kamihaya_cms' => 10009,
    ],
  ];
  return $dependencies;
}
