<?php

namespace PostenBringCheckout\admin;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class WeightUtils {

	public static function getWeightInKgs($weight) {
		return self::getWeightInGrams($weight) / 1000;
	}

	public static function getWeightInGrams($weight) {
		if (empty($weight)) {
			return 0;
		}

		if (!is_numeric($weight)) {
			wc_get_logger()->warning(
				"Weight was not numeric. Value given was " . $weight,
				array(
					'source'    => 'posten-bring-checkout',
					'backtrace' => true,
				)
			);
			return 0;
		}

		$weight = floatval($weight);

		$weight_unit = get_option('woocommerce_weight_unit');

		if ($weight_unit == 'kg') {
			return $weight * 1000;
		} else if ($weight_unit == 'lbs') {
			return $weight * 453.592;
		} else if ($weight_unit == 'oz') {
			return $weight * 28.3495;
		} else {
			return $weight;
		}
	}


}
