<?php

use Inoby\PostTypeBase;

add_action("init", function () {
  global $inoby_pt;
  $inoby_pt = [];

  $metaboxes = [];
  $thumb_retriever = new MbVideoThumbnailRetriever();

  foreach (get_declared_classes() as $class) {
    // register components
    if (is_subclass_of($class, MBComponentBase::class)) {
      /** @var MBComponentBase */
      $component = new $class($class);
      if ($mb = $component->get_mb()) {
        // register metaboxes
        $metaboxes[] = $mb;
        // register thumbnail retrieveing
        if ($component instanceof MBRetrievesVideoThumbnails) {
          $thumb_retriever->register($component);
        }
      }
      // register post types
    } elseif (is_subclass_of($class, PostTypeBase::class)) {
      /** @var PostTypeBase */
      $pt = new $class($class);
      // register post type
      $pt->register_pt();
      if ($mb = $pt->get_mb()) {
        // register metaboxes
        $metaboxes[] = $mb;
      }
      $inoby_pt[] = $pt;
    }
  }

  $thumb_retriever->run();

  add_filter("rwmb_meta_boxes", function ($mb) use ($metaboxes) {
    return array_merge($mb, $metaboxes);
  });
});
