<?php
/**
 * Trait ServicePointTrait
 *
 * @package DhlPolska\Point
 */

namespace DhlPolska\Point;

/**
 * Helpers for service point shipping method.
 */
trait ServicePointTrait {

	/**
	 * @return bool
	 */
	private function is_service_point_option(): bool {
		$service_point_rates = $this->get_service_point_rates();

		foreach ( $this->get_chosen_shipping_methods() as $chosen_shipping_method ) {
			if ( in_array( $chosen_shipping_method, $service_point_rates, true ) ) {
				return true;
			}
		}

		return false;
	}

	/**
	 * @return string[]
	 */
	private function get_chosen_shipping_methods(): array {
		$session = WC()->session;

		if ( null === $session ) { /** @phpstan-ignore-line */
			return [];
		}

		return wp_parse_list( $session->get( 'chosen_shipping_methods' ) );
	}

	/**
	 * @return string[]
	 */
	private function get_service_point_rates(): array {
		$session = WC()->session;

		if ( null === $session ) { /** @phpstan-ignore-line */
			return [];
		}

		return wp_parse_list( $session->get( CheckoutChoosePointField::SESSION_SERVICE_POINT_RATES, '' ) );
	}
}
