<?php declare(strict_types=1);

/**
 * This file is part of the Nette Framework (https://nette.org)
 * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
 */

namespace Nette\PhpGenerator;

use Nette;
use function array_map, func_num_args, is_object, str_contains, strtolower;


/**
 * Base definition of class, interface, trait or enum type.
 */
abstract class ClassLike
{
	use Traits\CommentAware;
	use Traits\AttributeAware;

	#[\Deprecated('Use Visibility::Public')]
	public const VisibilityPublic = Visibility::Public,
		VISIBILITY_PUBLIC = Visibility::Public;

	#[\Deprecated('Use Visibility::Protected')]
	public const VisibilityProtected = Visibility::Protected,
		VISIBILITY_PROTECTED = Visibility::Protected;

	#[\Deprecated('Use Visibility::Private')]
	public const VisibilityPrivate = Visibility::Private,
		VISIBILITY_PRIVATE = Visibility::Private;

	private ?PhpNamespace $namespace;
	private ?string $name;


	/** @param class-string|object  $class */
	public static function from(string|object $class, bool $withBodies = false): static
	{
		$instance = (new Factory)
			->fromClassReflection(new \ReflectionClass($class), $withBodies);

		if (!$instance instanceof static) {
			$class = is_object($class) ? $class::class : $class;
			throw new Nette\InvalidArgumentException("$class cannot be represented with " . static::class . '. Call ' . $instance::class . '::' . __FUNCTION__ . '() or ' . __METHOD__ . '() instead.');
		}

		return $instance;
	}


	public static function fromCode(string $code): static
	{
		$instance = (new Factory)
			->fromClassCode($code);

		if (!$instance instanceof static) {
			throw new Nette\InvalidArgumentException('Provided code cannot be represented with ' . static::class . '. Call ' . $instance::class . '::' . __FUNCTION__ . '() or ' . __METHOD__ . '() instead.');
		}

		return $instance;
	}


	public function __construct(string $name)
	{
		if (str_contains($name, '\\')) {
			$this->namespace = new PhpNamespace(Helpers::extractNamespace($name));
			$this->setName(Helpers::extractShortName($name));
		} else {
			$this->namespace = func_num_args() > 1 ? func_get_arg(1) : null; // backward compatibility
			$this->setName($name);
		}
	}


	public function __toString(): string
	{
		return (new Printer)->printClass($this, $this->namespace);
	}


	/** @internal */
	public function setNamespace(?PhpNamespace $namespace): static
	{
		$this->namespace = $namespace;
		return $this;
	}


	public function getNamespace(): ?PhpNamespace
	{
		return $this->namespace;
	}


	public function setName(?string $name): static
	{
		if ($name !== null && (!Helpers::isIdentifier($name) || isset(Helpers::Keywords[strtolower($name)]))) {
			throw new Nette\InvalidArgumentException("Value '$name' is not valid class name.");
		}

		$this->name = $name;
		return $this;
	}


	public function getName(): ?string
	{
		return $this->name;
	}


	public function getFullName(): ?string
	{
		return $this->name && ($namespace = $this->namespace?->getName())
			? $namespace . '\\' . $this->name
			: $this->name;
	}


	public function isClass(): bool
	{
		return $this instanceof ClassType;
	}


	public function isInterface(): bool
	{
		return $this instanceof InterfaceType;
	}


	public function isTrait(): bool
	{
		return $this instanceof TraitType;
	}


	public function isEnum(): bool
	{
		return $this instanceof EnumType;
	}


	/** @param list<string>  $names */
	protected function validateNames(array $names): void
	{
		foreach ($names as $name) {
			if (!Helpers::isNamespaceIdentifier($name, allowLeadingSlash: true)) {
				throw new Nette\InvalidArgumentException("Value '$name' is not valid class name.");
			}
		}
	}


	public function validate(): void
	{
	}


	public function __clone(): void
	{
		$this->attributes = array_map(fn($attr) => clone $attr, $this->attributes);
	}
}
