<?php

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/**
 * Implements addons for standard wiaas package types
 *
 * Class Wiaas_Package_Addon
 */
class Wiaas_Package_Addon {

	public static function init() {

		add_filter('wiaas_package_types', array( __CLASS__, 'register_addon_package_type' ) );
	}

	/**
	 * Registers addon as available wiaas package type
	 * @param $package_types
	 *
	 * @return array
	 */
	public static function register_addon_package_type($package_types) {
		$package_types[] = 'add_on';

		return $package_types;
	}

	/**
	 * Retrieves addons configured for provided wiaas standard package type
	 * @param $package
	 *
	 * @return array
	 */
	public static function get_package_addons($package) {
		$addon_ids = $package->get_meta( '__wiaas_package_addons' );

		$addons =  array();

		foreach ($addon_ids as $addon_id) {
			$addon_package = wc_get_product( $addon_id );
			if (is_object($addon_package)) {
				$addons[] = $addon_package;
			}
		}

		return $addons;
	}

	/**
	 * Retrieve ids of package addons
	 * @param int $package
	 *
	 * @return array
	 */
	public static function get_package_addons_ids($package) {
		return $package->get_meta( '__wiaas_package_addons' );
	}

	/**
	 * Sets addons for provided wiaas standard package type
	 * @param $package
	 * @param $children_ids
	 */
	public static function set_package_addons($package, $children_ids) {
		$package->update_meta_data( '__wiaas_package_addons', $children_ids );
		$package->save_meta_data();
	}
}

Wiaas_Package_Addon::init();