<?php
$align = isset($attributes['align']) ? $attributes['align'] : '';
$link = isset($attributes['link']) ? $attributes['link'] : [];
$text = isset($attributes['text']) ? $attributes['text'] : '';
$color = isset($attributes['color']) ? $attributes['color'] : '';
$target = (isset($link['opensInNewTab']) && $link['opensInNewTab']) ? '_blank' : '_self';
$raw_url = isset($link['url']) ? $link['url'] : '';
$is_inner_page_link = is_string($raw_url) && strpos($raw_url, '#') === 0;


$styles = [];
if ($color) {
	$styles[] = 'color: ' . esc_attr($color);
	$styles[] = '--cta-current-color: ' . esc_attr($color);
}

$wrapper_attributes = get_block_wrapper_attributes([
	'class' => 'align--' . esc_attr($align) . ($is_inner_page_link ? ' cta--inner-page-link' : ' cta--external-page-link'),
	'style' => implode('; ', $styles)
]);

$link_url = isset($link['url']) ? esc_url($link['url']) : '#';
?>

<a href="<?php echo $link_url; ?>" <?php echo $wrapper_attributes; ?> target="<?php echo $target; ?>">
	<?php echo esc_html($text); ?>
	<?php if ($is_inner_page_link) : ?>
		<svg id="arrow-internal-link" class="cta-icon" width="30" height="30" viewBox="0 0 30 30" fill="none" xmlns="http://www.w3.org/2000/svg">
			<g clip-path="url(#clip0_2159_20039)">
				<path d="M14.5469 20.4253L14.5469 10.5" stroke="#136F63" stroke-width="2" />
				<path d="M8.53276 14.1263L14.5536 20.4253L20.5684 14.1263" stroke="#136F63" stroke-width="2" />
			</g>
			<circle cx="15" cy="15" r="14" transform="rotate(90 15 15)" stroke="#136F63" stroke-width="2" />
			<defs>
				<clipPath id="clip0_2159_20039">
					<rect width="10.8" height="12.9" fill="white" transform="translate(21 10.5) rotate(90)" />
				</clipPath>
			</defs>
		</svg>


	<?php else : ?>
		<svg id="arrow-external-link" class="cta-icon" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32">
			<defs>
				<style>
					.cls-1 {
						fill: none;
						stroke: <?php echo esc_attr($color); ?>;
						stroke-width: 2px;
					}
				</style>
			</defs>
			<circle class="cls-1" cx="16" cy="16" r="15" />
			<g>
				<path class="cls-1" d="M21.15,10.59l-9.38,10.02" />
				<path class="cls-1" d="M21.15,21.4v-10.81h-10.32" />
			</g>
		</svg>

	<?php endif; ?>
</a>