<?php
if ( ! defined( 'ABSPATH' ) ) { exit; }

class KNSDD_Icon_Renderer {
    /**
     * İkon render helper
     */
    public function render_icon( $icon ) {
        if ( empty( $icon['enabled'] ) ) return;
        $style = '';
        if ( ! empty( $icon['color'] ) ) $style .= 'color:' . esc_attr( $icon['color'] ) . ';fill:' . esc_attr( $icon['color'] ) . ';';
        $size_key = in_array( $icon['size'], array('sm','md','lg'), true ) ? $icon['size'] : 'md';
        $size_class = 'knsdd-icon--' . $size_key;

        // Iconify pack: KSES'e takılmaması için custom element yerine span.iconify kullan
        if ( isset( $icon['pack'] ) && $icon['pack'] === 'iconify' && ! empty( $icon['value'] ) ) {
            echo '<span class="knsdd-icon iconify ' . esc_attr( $size_class ) . '" data-icon="' . esc_attr( $icon['value'] ) . '" style="' . esc_attr( $style ) . '"></span>';
            return;
        }

        // Geri uyumluluk: preset SVG seti
        echo '<span class="knsdd-icon ' . esc_attr( $size_class ) . '" aria-hidden="true" style="' . esc_attr( $style ) . '">';
        $key = '';
        if ( isset( $icon['pack'] ) && $icon['pack'] === 'preset' && ! empty( $icon['value'] ) ) {
            $key = $icon['value'];
        }
        if ( empty( $key ) ) {
            $key = 'check'; // güvenli varsayılan preset
        }
        $svg = $this->get_preset_icon_svg( $key );
        if ( $svg ) {
            // Boyutu sm/md/lg'ye gore px cinsinden hesapla ve inline stil olarak enjekte et (temadaki tum kurallari ezer)
            switch ( $size_key ) {
                case 'sm': $px = 12; break;
                case 'lg': $px = 20; break;
                case 'md':
                default:   $px = 16; break;
            }
            $inline_svg_style = 'width:' . $px . 'px !important;height:' . $px . 'px !important;display:block;';
            // Genişlik/yükseklik ve temel inline stil
            $svg = preg_replace( '/<svg\s+/', '<svg width="' . $px . 'px" height="' . $px . 'px" style="' . $inline_svg_style . '" ', $svg, 1 );
            // DOLGU tabanlı ikonları koru: preset SVG'ler zaten fill="currentColor" içerir.
            echo $svg;
        }
        echo '</span>';
    }

    /**
     * YENİ: İkon için HTML return eder (WooCommerce notice içinde kullanım için).
     */
    public function get_icon_html( $icon ) {
        if ( empty( $icon['enabled'] ) ) {
            return '';
        }
        
        $style = '';
        if ( ! empty( $icon['color'] ) ) {
            $style .= 'color:' . esc_attr( $icon['color'] ) . ';fill:' . esc_attr( $icon['color'] ) . ';';
        }
        
        $size_key = in_array( $icon['size'], array('sm','md','lg'), true ) ? $icon['size'] : 'md';
        $size_class = 'knsdd-icon--' . $size_key;

        // Iconify pack: KSES'e takılmaması için custom element yerine span.iconify kullan
        if ( isset( $icon['pack'] ) && $icon['pack'] === 'iconify' && ! empty( $icon['value'] ) ) {
            return '<span class="knsdd-icon iconify ' . esc_attr( $size_class ) . '" data-icon="' . esc_attr( $icon['value'] ) . '" style="' . esc_attr( $style ) . '"></span>';
        }

        // Geri uyumluluk: preset SVG seti
        $html = '<span class="knsdd-icon ' . esc_attr( $size_class ) . '" aria-hidden="true" style="' . esc_attr( $style ) . '">';

        // İkon SVG'sini al
        $key = '';
        if ( isset( $icon['pack'] ) && $icon['pack'] === 'preset' && ! empty( $icon['value'] ) ) {
            $key = $icon['value'];
        }
        if ( empty( $key ) ) {
            $key = 'check'; // güvenli varsayılan preset
        }

        $svg = $this->get_preset_icon_svg( $key );
        if ( $svg ) {
            // Boyutu hesapla
            switch ( $size_key ) {
                case 'sm': $px = 12; break;
                case 'lg': $px = 20; break;
                case 'md':
                default:   $px = 16; break;
            }
            $inline_svg_style = 'width:' . $px . 'px !important;height:' . $px . 'px !important;display:block;';
            $svg = preg_replace( '/<svg\s+/', '<svg width="' . $px . 'px" height="' . $px . 'px" style="' . $inline_svg_style . '" ', $svg, 1 );
            $html .= $svg;
        }

        $html .= '</span>';

        return $html;
    }

    /**
     * Preset ikon SVG sözlüğü.
     * fill="currentColor" kullanılır; boyut CSS ile ayarlanır (1em).
     */
    public function get_preset_icon_svg( $key ) {
        $icons = array(
            'check'      => '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" fill="currentColor"><path d="M9 16.2l-3.5-3.5L4 14.2l5 5 11-11-1.5-1.5z"/></svg>',
            'sale'       => '<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" fill="currentColor"><path d="M3 12l9-9 9 9-9 9-9-9zm9-5a1 1 0 110 2 1 1 0 010-2zm0 4a4 4 0 100 8 4 4 0 000-8z"/></svg>',
            'percent'    => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M18.5 5.5l-13 13 1 1 13-13-1-1zM8 7a2 2 0 110 4 2 2 0 010-4zm8 6a2 2 0 110 4 2 2 0 010-4z"/></svg>',
            'tag'        => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M10 3L3 10l7 7 7-7V3h-7zm2 6a2 2 0 110-4 2 2 0 010 4z"/></svg>',
            'megaphone'  => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M2 10v4l12-4V6L2 10zm14-4v12l6 2V4l-6 2zM8 16l1.5 4h3L10.5 16H8z"/></svg>',
            'clock'      => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M12 2a10 10 0 100 20 10 10 0 000-20zm1 11H7v-2h4V5h2v8z"/></svg>',
            'calendar'   => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M7 2h2v2h6V2h2v2h3v18H4V4h3V2zm13 8H4v10h16V10z"/></svg>',
            'truck'      => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M3 6h11v7h2l3 3v2h-1a2 2 0 11-4 0H8a2 2 0 11-4 0H3V6zm15 8h1.586L18 12.414V14z"/></svg>',
            'gift'       => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M20 7h-3.17A3 3 0 0012 3a3 3 0 00-4.83 4H4v4h16V7zm-9-2a1 1 0 110 2H8a1 1 0 110-2zm6 2h-3a1 1 0 110-2 1 1 0 011 1v1zM4 13h7v8H4v-8zm9 0h7v8h-7v-8z"/></svg>',
            'star'       => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M12 2l3.09 6.26L22 9.27l-5 4.87L18.18 22 12 18.56 5.82 22 7 14.14l-5-4.87 6.91-1.01L12 2z"/></svg>',
            'cart'       => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M7 18a2 2 0 100 4 2 2 0 000-4zm10 0a2 2 0 100 4 2 2 0 000-4zM7.2 14h9.9l2.7-8H6.2L5.3 3H2v2h2l3.6 9z"/></svg>',
            'coupon'     => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M3 7h18v4a2 2 0 100 4v4H3v-4a2 2 0 100-4V7zm8 2H8v2h3V9zm5 0h-3v2h3V9z"/></svg>',
            'badge'      => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M12 2l3 5 6 1-4 4 1 6-6-3-6 3 1-6-4-4 6-1 3-5z"/></svg>',
            'flame'      => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M13 3s4 3 4 7a5 5 0 11-10 0c0-2 1-4 3-5-1 3 3 3 3-2z"/></svg>',
            'rocket'     => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M12 2c4 0 8 4 8 8-4 0-8 4-8 8-4 0-8-4-8-8 4 0 8-4 8-8zM5 19l3-1-2-2-1 3z"/></svg>',
            'wallet'     => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M3 6h18v12H3V6zm14 5h3v2h-3v-2z"/></svg>',
            'cash'       => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M2 7h20v10H2V7zm4 2a2 2 0 01-2 2v2a2 2 0 012 2h12a2 2 0 012-2v-2a2 2 0 01-2-2H6z"/></svg>',
            'bell'       => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M12 22a2 2 0 002-2H10a2 2 0 002 2zm6-6V11a6 6 0 10-12 0v5l-2 2h16l-2-2z"/></svg>',
            'info'       => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M11 17h2v-6h-2v6zm0-8h2V7h-2v2zm1-7a10 10 0 100 20 10 10 0 000-20z"/></svg>',
            'truckFast'  => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M3 13h8v-2H4l-1 2zm13-6v7h2l3 3v2h-2a2 2 0 11-4 0H9a2 2 0 11-4 0H3v-2h1l1-2h11z"/></svg>',
            'lightning'  => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M13 2L3 14h7l-1 8 10-12h-7l1-8z"/></svg>',
            'medal'      => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M7 2h10l-2 6H9L7 2zm5 8a6 6 0 100 12 6 6 0 000-12z"/></svg>',
            'ribbon'     => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M12 2a5 5 0 100 10 5 5 0 000-10zM6 22l6-4 6 4-6-10-6 10z"/></svg>',
            'box'        => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M3 7l9-4 9 4-9 4-9-4zm0 3l9 4 9-4v7l-9 4-9-4V10z"/></svg>',
            'refresh'    => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M17.65 6.35A8 8 0 104 12h2a6 6 0 116-6V3l4 4-4 4V7a5 5 0 00-5 5h2a3 3 0 013-3 3 3 0 013 3h2a5 5 0 00-5-5z"/></svg>',
            'crown'      => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M2 7l4 4 6-6 6 6 4-4v10H2V7z"/></svg>',
            'heart'      => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M12 21s-6-4.35-9-7.35C-1 12 2 4 7 7c2 1 3 3 5 5 2-2 3-4 5-5 5-3 8 5 4 6.65C18 16.65 12 21 12 21z"/></svg>',
            'target'     => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M12 2v2a8 8 0 108 8h2A10 10 0 1112 2zm0 4a6 6 0 106 6h-2a4 4 0 11-4-4V6z"/></svg>',
            'spark'      => '<svg viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg"><path d="M12 2l2 6 6 2-6 2-2 6-2-6-6-2 6-2 2-6z"/></svg>',
        );
        return isset( $icons[ $key ] ) ? $icons[ $key ] : '';
    }
}
