<?php

use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;

/**
 * Update configs.
 */
function kamihaya_cms_block_type_update_10001() {
  if (empty(BlockContentType::load('kamihaya_block'))) {
    return;
  }
  _kamihaya_cms_common_add_paragraph('block_content', 'kamihaya_block', 'block', 'field_body');
  _kamihaya_cms_common_add_paragraph('block_content', 'kamihaya_block', 'views', 'field_body');
}

/**
 * Update configs.
 */
function kamihaya_cms_block_type_update_10002() {
  if (empty(BlockContentType::load('kamihaya_block'))) {
    return;
  }
  _kamihaya_cms_common_add_paragraph('block_content', 'kamihaya_block', 'border_box', 'field_body');
}

/**
 * Update configs.
 */
function kamihaya_cms_block_type_update_10003() {
  $block = \Drupal\block\Entity\Block::load('maintenance_mode');
  if (!empty($block)) {
    return;
  }

  $file_config = _kamihaya_cms_common_load_config_file('kamihaya_cms_block_type', 'block.block.maintenance_mode.yml', 'install');
  $title = !empty($file_config['settings']['label']) ? $file_config['settings']['label'] : '';
  $id = !empty($file_config['settings']['id']) && strpos($file_config['settings']['id'], ':') !== FALSE ? explode(':', $file_config['settings']['id'])[1] : '';
  if (empty($title) || empty($id)) {
    return;
  }
  $block_content = BlockContent::create([
    'type' => 'kamihaya_block',
    'info' => $title,
    'uuid' => $id,
  ]);
  $block_content->save();

  _kamihaya_cms_common_change_configs('kamihaya_cms_block_type', [
    'block.block.maintenance_mode.yml',
  ], 'install');
}

/**
 * Update configs.
 */
function kamihaya_cms_block_type_update_10004() {
  _kamihaya_cms_common_add_paragraph('block_content', 'kamihaya_block', 'anchor_link', 'field_body');
}

/**
 * Update configs.
 */
function kamihaya_cms_block_type_update_10005() {
  _kamihaya_cms_common_add_paragraph('block_content', 'kamihaya_block', 'background_image', 'field_body');
}

/**
 * Implements hook_update_dependencies().
 */
function kamihaya_cms_block_type_update_dependencies() {
  $dependencies['kamihaya_cms_block_type'] = [
    10001 => [
      'kamihaya_cms_paragraph_type' => 10007,
    ],
    10002 => [
      'kamihaya_cms_paragraph_type' => 10008,
    ],
    10004 => [
      'kamihaya_cms_paragraph_type' => 10012,
    ],
    10005 => [
      'kamihaya_cms_paragraph_type' => 10013,
    ],
  ];
  return $dependencies;
}
