<?php
/**
 * Product URL draft.
 *
 * @package Affilun\Application\Product
 */

declare(strict_types=1);

namespace Affilun\Application\Product;

use InvalidArgumentException;

/**
 * URL association requested while saving a product.
 */
final class ProductUrlDraft {

	/**
	 * Provider namespace for the URL.
	 */
	private string $providerType;

	/**
	 * Optional provider-side item identifier.
	 */
	private ?string $providerItemId;

	/**
	 * Product or affiliate URL.
	 */
	private string $url;

	/**
	 * Source that produced the URL draft.
	 */
	private string $source;

	/**
	 * @param string      $providerType   Provider namespace.
	 * @param string|null $providerItemId Optional provider-side item identifier.
	 * @param string      $url            Product or affiliate URL.
	 * @param string      $source         Source that produced the URL.
	 */
	public function __construct( string $providerType, ?string $providerItemId, string $url, string $source = 'user' ) {
		if ( trim( $providerType ) === '' ) {
			throw new InvalidArgumentException( 'ProductUrlDraft providerType must be non-empty.' );
		}
		if ( trim( $url ) === '' ) {
			throw new InvalidArgumentException( 'ProductUrlDraft url must be non-empty.' );
		}
		if ( trim( $source ) === '' ) {
			throw new InvalidArgumentException( 'ProductUrlDraft source must be non-empty.' );
		}

		$this->providerType   = trim( $providerType );
		$this->providerItemId = $providerItemId === null || trim( $providerItemId ) === '' ? null : trim( $providerItemId );
		$this->url            = trim( $url );
		$this->source         = trim( $source );
	}

	/**
	 * Return the provider namespace.
	 */
	public function providerType(): string {
		return $this->providerType;
	}

	/**
	 * Return the optional provider-side item identifier.
	 */
	public function providerItemId(): ?string {
		return $this->providerItemId;
	}

	/**
	 * Return the product or affiliate URL.
	 */
	public function url(): string {
		return $this->url;
	}

	/**
	 * Return the source that produced the URL.
	 */
	public function source(): string {
		return $this->source;
	}
}
