<?php

/**
 * Collect optional packages for provided order item package
 * @param $order_items
 * @param $parent_order_item
 *
 * @return array
 */
function wiaas_get_order_item_options($order_items, $parent_order_item) {
	$option_order_items = array();

	if (isset($parent_order_item['wiaas_option_items']) && isset($parent_order_item['bundle_cart_key'])) {
		foreach ($order_items as $order_item) {
			if (isset($order_item['bundle_cart_key']) && $order_item['wiaas_option_for'] === $parent_order_item['bundle_cart_key']) {
				$option_order_items[] = $order_item;
			}
		}
	}

	return $option_order_items;
}

/**
 * Collect optional packages cart items for provided cart item
 * @param $cart_item
 *
 * @return array
 */
function wiaas_get_cart_item_options($cart_item) {
	$option_cart_items = array();

	if (isset($cart_item['_wiaas_option_items'])) {
		$option_cart_items_ids = $cart_item['_wiaas_option_items'];

		foreach ($option_cart_items_ids as $option_cart_item_id) {
			$option_cart_item  = WC()->cart->get_cart_item($option_cart_item_id);

			if (isset($option_cart_item)) {
				$option_cart_items[] = $option_cart_item;
			}
		}
	}

	return $option_cart_items;
}

/**
 * Collect addon packages for provided order item package
 * @param $order_items
 * @param $parent_order_item
 *
 * @return array
 */
function wiaas_get_order_item_addons($order_items, $parent_order_item) {
	$addon_order_items = array();

	if (isset($parent_order_item['wiaas_addon_items']) && isset($parent_order_item['bundle_cart_key'])) {
		foreach ($order_items as $order_item) {
			if (isset($order_item['bundle_cart_key']) && $order_item['wiaas_addon_for'] === $parent_order_item['bundle_cart_key']) {
				$addon_order_items[] = $order_item;
			}
		}
	}

	return $addon_order_items;
}

/**
 * Collect addon packages cart items for provided cart item
 * @param $cart_item
 *
 * @return array
 */
function wiaas_get_cart_item_addons($cart_item) {
	$addon_cart_items = array();

	if (isset($cart_item['_wiaas_addon_items'])) {
		$addon_cart_items_ids = $cart_item['_wiaas_addon_items'];

		foreach ($addon_cart_items_ids as $addon_cart_item_id) {
			$addon_cart_item  = WC()->cart->get_cart_item($addon_cart_item_id);

			if (isset($addon_cart_item)) {
				$addon_cart_items[] = $addon_cart_item;
			}
		}
	}

	return $addon_cart_items;
}

/**
 * Check if order item is wiaas standard package
 * @param WC_Order_Item $item
 *
 * @return bool
 */
function wiaas_is_order_item__standard_package($item) {
	return isset($item['wiaas_standard_package']);
}