<?php

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

function tilfy_ikon_url( $ad ) {
	$ad = sanitize_file_name( (string) $ad );
	if ( $ad === '' ) {
		return '';
	}
	$dosya = TILFY_YOL . 'assets/' . $ad . '.svg';
	if ( ! is_readable( $dosya ) ) {
		return '';
	}
	return TILFY_URL . 'assets/' . $ad . '.svg';
}

function tilfy_ikon_img( $ad, $attrs = array() ) {
	$url = tilfy_ikon_url( $ad );
	if ( $url === '' ) {
		return '';
	}
	$class = trim( 'tilfy-ikon ' . (string) ( $attrs['class'] ?? '' ) );
	$alt   = (string) ( $attrs['alt'] ?? '' );
	$title = (string) ( $attrs['title'] ?? '' );
	$html  = '<img src="' . esc_url( $url ) . '" class="' . esc_attr( $class ) . '" alt="' . esc_attr( $alt ) . '" loading="lazy" decoding="async"';
	if ( $title !== '' ) {
		$html .= ' title="' . esc_attr( $title ) . '"';
	}
	$html .= ' />';
	return $html;
}

function tilfy_ikon_img_allowed_html() {
	return array(
		'img' => array(
			'src'      => true,
			'class'    => true,
			'alt'      => true,
			'title'    => true,
			'loading'  => true,
			'decoding' => true,
		),
	);
}

function tilfy_ikon_img_e( $ad, $attrs = array() ) {
	echo wp_kses( tilfy_ikon_img( $ad, $attrs ), tilfy_ikon_img_allowed_html() );
}

function tilfy_platform_ikon_ad( $platform ) {
	$map = array(
		'instagram' => 'instagram',
		'facebook'  => 'facebook',
		'youtube'   => 'youtube',
		'linkedin'  => 'linkedin',
		'tiktok'    => 'tiktok',
		'threads'   => 'threads',
		'wordpress' => 'wordpress',
	);
	$platform = sanitize_key( (string) $platform );
	return $map[ $platform ] ?? '';
}

function tilfy_ikonlar_listesi() {
	$adlar = array( 'ekle', 'studyoda-ac', 'instagram', 'facebook', 'youtube', 'linkedin', 'tiktok', 'threads', 'wordpress' );
	$cikti = array();
	foreach ( $adlar as $ad ) {
		$url = tilfy_ikon_url( $ad );
		if ( $url !== '' ) {
			$cikti[ $ad ] = $url;
		}
	}
	return $cikti;
}

function tilfy_icerik_medya_grup( $medya_tipi ) {
	$tip    = strtolower( sanitize_key( (string) $medya_tipi ) );
	$gorsel = array( 'image', 'photo', 'carousel_album', 'gorsel', 'thumbnail', 'album' );
	if ( in_array( $tip, $gorsel, true ) ) {
		return 'gorsel';
	}
	return 'video';
}

function tilfy_icerik_medya_grup_etiket( $medya_tipi ) {
	return tilfy_icerik_medya_grup( $medya_tipi ) === 'gorsel' ? 'Görsel' : 'Video';
}
