<?php
/**
 * File: BasketItem.php
 * Year: 2023
 */

namespace Tami\WooCommerce;
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if(!class_exists('\Tami\WooCommerce\BasketItem')) {
	class BasketItem {
		public string $itemId;
		public string $itemType;
		public string $name;
		public string $category = "";
		public string $subCategory = "";
		public string $unitPrice = "1";
		public string $totalPrice;
		public int $numberOfProducts;

		public function setItemId( string $itemId ): BasketItem {
			$this->itemId = $itemId;

			return $this;
		}

		public function setItemType( string $itemType ): BasketItem {
			$this->itemType = $itemType;

			return $this;
		}

		public static function filter_string( $input ) {
			$input = htmlspecialchars_decode( $input );

			return preg_replace( "/[^a-zA-Z0-9:\/\-çÇıİğĞöÖşŞüÜ]+/", " ", $input );
		}

		public function setName( string $name ): BasketItem {
			$this->name = mb_substr(self::filter_string($name), 0, 50);
			return $this;
		}

		public function setCategory( string $category ): BasketItem {
			$this->category = $category;

			return $this;
		}

		public function setSubCategory( string $subCategory ): BasketItem {
			$this->subCategory = $subCategory;

			return $this;
		}

		public function setUnitPrice( float $unitPrice ): BasketItem {
			$this->unitPrice = number_format($unitPrice, 2, '.','');

			return $this;
		}

		public function setTotalPrice( float $totalPrice ): BasketItem {
			$this->totalPrice = number_format($totalPrice, 2, '.','');

			return $this;
		}

		public function setNumberOfProducts( int $numberOfProducts ): BasketItem {
			$this->numberOfProducts = $numberOfProducts;

			return $this;
		}

		public function get(): object {
			return (object) [
				'itemId'           => $this->itemId,
				'itemType'         => $this->itemType,
				'name'             => $this->name,
				'category'         => $this->category,
				'subCategory'      => $this->subCategory,
				'unitPrice'        => $this->unitPrice,
				'totalPrice'       => $this->totalPrice,
				'numberOfProducts' => $this->numberOfProducts
			];
		}
	}
}
