<?php

use Drupal\commerce_product\Entity\ProductType;
use Drupal\paragraphs\Entity\ParagraphsType;

/**
 * Implements hook_install().
 */
function kamihaya_cms_commerce_install() {
  _kamihaya_cms_common_change_configs('kamihaya_cms_commerce', [
    'core.entity_form_display.commerce_product.default.default.yml',
    'core.entity_view_display.commerce_product.default.default.yml',
  ], 'optional');
}

/**
 * Update configs.
 */
function kamihaya_cms_commerce_update_10001() {
  _kamihaya_cms_common_change_configs('kamihaya_cms_commerce', [
    'taxonomy.vocabulary.product_category.yml',
    'core.entity_form_display.taxonomy_term.product_category.default.yml',
    'language.content_settings.taxonomy_term.product_category.yml',
  ], 'install');

  _kamihaya_cms_common_change_configs('kamihaya_cms_commerce', [
    'field.storage.commerce_product.field_body.yml',
    'field.storage.commerce_product.field_category.yml',
    'field.storage.commerce_product.field_description.yml',
    'field.storage.commerce_product.field_thumbnail_image.yml',
    'field.field.commerce_product.default.field_body.yml',
    'field.field.commerce_product.default.field_category.yml',
    'field.field.commerce_product.default.field_description.yml',
    'field.field.commerce_product.default.field_thumbnail_image.yml',
    'core.entity_form_display.commerce_product.default.default.yml',
    'core.entity_view_display.commerce_product.default.default.yml',
  ], 'optional');

}

/**
 * Update configs.
 */
function kamihaya_cms_commerce_update_10002() {
  \Drupal::service('module_installer')->install(['shs']);
  _kamihaya_cms_common_change_configs('kamihaya_cms_commerce', [
    'field.storage.taxonomy_term.field_thumbnail_image.yml',
    'field.storage.taxonomy_term.field_path_name.yml',
    'field.storage.taxonomy_term.field_direct_link.yml',
    'field.field.taxonomy_term.product_category.field_thumbnail_image.yml',
    'field.field.taxonomy_term.product_category.field_path_name.yml',
    'field.field.taxonomy_term.product_category.field_direct_link.yml',
    'core.entity_view_display.taxonomy_term.product_category.default.yml',
    'core.entity_form_display.taxonomy_term.product_category.default.yml',
  ], 'install');

    _kamihaya_cms_common_change_configs('kamihaya_cms_commerce', [
    'pathauto.pattern.product_page.yml',
    'core.entity_view_mode.commerce_product.product_list.yml',
    'core.entity_view_display.commerce_product.default.product_list.yml',
    'views.view.product_list.yml',
  ], 'optional');

  _kamihaya_cms_common_save_translation_configs([
    'field.field.commerce_product.default.field_body.yml',
    'field.field.commerce_product.default.field_category.yml',
    'field.field.commerce_product.default.field_description.yml',
    'field.field.commerce_product.default.field_thumbnail_image.yml',
    'field.field.taxonomy_term.product_category.field_thumbnail_image.yml',
    'field.field.taxonomy_term.product_category.field_path_name.yml',
    'field.field.taxonomy_term.product_category.field_direct_link.yml',
    'taxonomy.vocabulary.product_category.yml',
    'views.view.product_list.yml',
  ], 'ja');
}

/**
 * Update configs.
 */
function kamihaya_cms_commerce_update_10003() {
  _kamihaya_cms_common_change_configs('kamihaya_cms_commerce', [
    'field.storage.commerce_product.field_display_description.yml',
    'field.field.commerce_product.default.field_body.yml',
    'field.field.commerce_product.default.field_display_description.yml',
    'core.entity_form_display.commerce_product.default.default.yml',
    'core.entity_view_display.commerce_product.default.default.yml',
    'views.view.product_list.yml',
  ], 'optional');
}

/**
 * Update configs.
 */
function kamihaya_cms_commerce_update_10004() {
  if (empty(ProductType::load('default'))) {
    return;
  }
  _kamihaya_cms_common_add_paragraph('commerce_product', 'default', 'border_box', 'field_body');
}

/**
 * Update configs.
 */
function kamihaya_cms_commerce_update_10005() {
  _kamihaya_cms_common_add_paragraph('commerce_product', 'default', 'anchor_link', 'field_body');
}

/**
 * Update configs.
 */
function kamihaya_cms_commerce_update_10006() {
  _kamihaya_cms_common_add_paragraph('commerce_product', 'default', 'background_image', 'field_body');
}


/**
 * Implements hook_update_dependencies().
 */
function kkamihaya_cms_commerce_update_dependencies() {
  $dependencies['kamihaya_cms_commerce'] = [
    10004 => [
      'kamihaya_cms_paragraph_type' => 10008,
    ],
    10005 => [
      'kamihaya_cms_paragraph_type' => 10012,
    ],
    10006 => [
      'kamihaya_cms_paragraph_type' => 10013,
    ],
  ];
  return $dependencies;
}
