<?php

namespace PostenBringCheckout\admin\bring\models\service;

defined( 'ABSPATH' ) || exit;

class MaxDimensions {
	public int $height;
	public int $width;
	public int $length;

	public function __construct( array $data ) {
		$this->height = $data['height'];
		$this->width  = $data['width'];
		$this->length = $data['length'];
	}

	public function sorted(): array {
		$dimensions = array( $this->height, $this->width, $this->length );
		rsort( $dimensions );
		return $dimensions;
	}

	public static function fits_within( ?MaxDimensions $max_dimensions, int $height, int $width, int $length ): bool {
		$max      = ! empty( $max_dimensions ) ? $max_dimensions->sorted() : null;
		$provided = array( $height, $width, $length );
		rsort( $provided );

		return empty( $max_dimensions ) || ( ! empty( $max ) && $max[0] >= $provided[0] && $max[1] >= $provided[1] && $max[2] >= $provided[2] );
	}
}
