<?php

namespace PostenBringCheckout\checkout\integrations\shipmondo;



use WC_Order;

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

/**
 * Shipmondo integration
 *
 * Class responsible for handling all Shipmondo integration specifics
 *
 * @since 0.1.19
 */
class ShipmondoIntegration {

	/**
	 * Registers the selected pickup point details as meta_data in a format Shipmondo can read
	 *
	 * @param array $selected_pickup_point The selected pickup point.
	 * @param WC_Order $order WooCommerce order.
	 * @since 0.1.19
	 */
	public static function add_pickup_point_details( array $selected_pickup_point, $order) {
		$address = explode( ', ', $selected_pickup_point['address'] );
		preg_match( '/^(\d{4,5})\s*(.*)$/', $address[1], $addressParts );
		$order->update_meta_data(
			'shipmondo_pickup_point',
			array(
				'name'         => $selected_pickup_point['displayName'],
				'address_1'    => $address[0],
				'city'         => $addressParts[2],
				'postcode'     => $addressParts[1],
				'country'      => $order->get_shipping_country(),
				'carrier_code' => 'bring',
				'id'           => $selected_pickup_point['id'],
			)
		);

		$order->save();
	}
}
