<?php

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

/**
 * Implements hook_install().
 */
function nodehive_area_fragment_install() {
  $block_storage = \Drupal::entityTypeManager()
    ->getStorage('block');

  /** @var \Drupal\block\BlockInterface $block */
  $block = $block_storage->create(
    [
      'id' => 'nodehive_area_fragment_connected_spaces_block',
      'plugin' => 'nodehive_area_fragment_connected_spaces_block',
      'theme' => 'gin',
    ]
  );

  $block->setRegion('highlighted');
  $block->setWeight(10);
  $block->save();

  $settings = \Drupal::configFactory()
    ->getEditable('nodehive_area_fragment.settings');

  $settings->set('connected_space_block_uuid', $block->uuid());
  $settings->save();
}

/**
 * Implements hook_uninstall().
 */
function nodehive_area_fragment_uninstall() {
  $settings = \Drupal::configFactory()->get('nodehive_area_fragment.settings');
  $block_uuid = $settings->get('connected_space_block_uuid');

  if (!$block_uuid) {
    return;
  }

  $block_content = \Drupal::service('entity.repository')
    ->loadEntityByUuid('block_content', $block_uuid);

  if ($block_content) {
    $block_content->delete();
  }
}
