<?php

$data = $attributes["data"];
$pt = $data["post_type"] ?? "post";

$options = $data["options"] ?? [];
$infinite = in_array("infinite", $options);
$auto = in_array("auto", $options);
$arrows = in_array("arrows", $options);
$dots = in_array("dots", $options);

$cfg = Inoby_Config::component("posts-slider");
$slider_default = $cfg["slider"]["default"] ?? [];
$slider = wp_parse_args($cfg["slider"][$pt] ?? [], $slider_default);

$posts = array_reduce(
  $data["posts"],
  function ($ids, $p) {
    $id = $p["post"] ?? 0;
    if ($id) {
      $ids[] = $id;
    }
    return $ids;
  },
  [],
);

$card = $slider["template-part"] ?? "" ?: "post-types/$pt/parts/card";

$slider_id = uniqid("posts-slider-");
// nemam posty - nic nevykreslim
if (!count($posts)) {
  return;
}
$attributes_class_name = $attributes["className"] ?? "";
$classes = "inoby-posts-slider posts-slider-$pt " . $attributes_class_name;
?>
<section <?= inoby_block_attrs($attributes, ["class" => $classes]) ?>>
  <div class="container">
    <div <?= inoby_block_attrs($attributes, [
      "class" => "keen-slider",
      "id" => $slider_id,
      "data-infinite" => $infinite,
      "data-slides" => $slider["slides"],
      "data-slides-md" => $slider["slides-md"],
      "data-slides-sm" => $slider["slides-sm"],
      "data-spacing" => $slider["spacing"],
      "data-spacing-md" => $slider["spacing-md"],
      "data-spacing-sm" => $slider["spacing-sm"],
      "data-origin" => $slider["origin"],
      "data-origin-md" => $slider["origin-md"],
      "data-origin-sm" => $slider["origin-sm"],
      "data-automatic" => $auto,
      "data-arrows" => $arrows,
      "data-dots" => $dots,
    ]) ?>>
      <?php foreach ($posts as $id) {
        inoby_setup_postdata($id);
        get_template_part($card, null, ["post" => $id]);
        inoby_reset_post_data();
      } ?>
    </div>

  </div>
</section>