<?php
namespace App\Core;
class CodeGenerator {
    private static array $map=['0'=>'nnnwwnwnn','1'=>'wnnwnnnnw','2'=>'nnwwnnnnw','3'=>'wnwwnnnnn','4'=>'nnnwwnnnw','5'=>'wnnwwnnnn','6'=>'nnwwwnnnn','7'=>'nnnwnnwnw','8'=>'wnnwnnwnn','9'=>'nnwwnnwnn','A'=>'wnnnnwnnw','B'=>'nnwnnwnnw','C'=>'wnwnnwnnn','D'=>'nnnnwwnnw','E'=>'wnnnwwnnn','F'=>'nnwnwwnnn','G'=>'nnnnnwwnw','H'=>'wnnnnwwnn','I'=>'nnwnnwwnn','J'=>'nnnnwwwnn','K'=>'wnnnnnnww','L'=>'nnwnnnnww','M'=>'wnwnnnnwn','N'=>'nnnnwnnww','O'=>'wnnnwnnwn','P'=>'nnwnwnnwn','Q'=>'nnnnnnwww','R'=>'wnnnnnwwn','S'=>'nnwnnnwwn','T'=>'nnnnwnwwn','U'=>'wwnnnnnnw','V'=>'nwwnnnnnw','W'=>'wwwnnnnnn','X'=>'nwnnwnnnw','Y'=>'wwnnwnnnn','Z'=>'nwwnwnnnn','-'=>'nwnnnnwnw','.'=>'wwnnnnwnn',' '=>'nwwnnnwnn','$'=>'nwnwnwnnn','/'=>'nwnwnnnwn','+'=>'nwnnnwnwn','%'=>'nnnwnwnwn','*'=>'nwnnwnwnn'];
    public static function barcodeSvg(string $text, int $height=72): string { $safe=preg_replace('/[^0-9A-Z\-\. \$\/+%]/','-',strtoupper($text)); $enc='*'.$safe.'*'; $x=10; $bars=''; foreach(str_split($enc) as $ch){ $pat=self::$map[$ch]??self::$map['-']; foreach(str_split($pat) as $i=>$w){ $bw=$w==='w'?3:1; if($i%2===0) $bars.='<rect x="'.$x.'" y="10" width="'.$bw.'" height="'.$height.'" fill="#111"/>'; $x+=$bw; } $x+=1; } $w=$x+10; return '<svg xmlns="http://www.w3.org/2000/svg" width="'.$w.'" height="'.($height+42).'" viewBox="0 0 '.$w.' '.($height+42).'">'.$bars.'<text x="'.($w/2).'" y="'.($height+30).'" text-anchor="middle" font-family="monospace" font-size="14">'.htmlspecialchars($safe,ENT_QUOTES,'UTF-8').'</text></svg>'; }
    public static function qrSvg(string $payload, int $size=220): string { $cells=29; $cell=max(4,intdiv($size,$cells)); $real=$cell*$cells; $hash=hash('sha256',$payload); $bits=''; foreach(str_split($hash) as $h){ $bits.=str_pad(base_convert($h,16,2),4,'0',STR_PAD_LEFT); } $svg='<svg xmlns="http://www.w3.org/2000/svg" width="'.$real.'" height="'.$real.'" viewBox="0 0 '.$real.' '.$real.'" data-payload="'.htmlspecialchars($payload,ENT_QUOTES,'UTF-8').'"><rect width="100%" height="100%" fill="#fff"/>'; $finder=function($x,$y) use (&$svg,$cell){ $svg.='<rect x="'.($x*$cell).'" y="'.($y*$cell).'" width="'.(7*$cell).'" height="'.(7*$cell).'" fill="#111"/><rect x="'.(($x+1)*$cell).'" y="'.(($y+1)*$cell).'" width="'.(5*$cell).'" height="'.(5*$cell).'" fill="#fff"/><rect x="'.(($x+2)*$cell).'" y="'.(($y+2)*$cell).'" width="'.(3*$cell).'" height="'.(3*$cell).'" fill="#111"/>'; }; $finder(0,0); $finder(22,0); $finder(0,22); $i=0; $len=max(1,strlen($payload)); for($y=0;$y<$cells;$y++){ for($x=0;$x<$cells;$x++){ if(($x<8&&$y<8)||($x>20&&$y<8)||($x<8&&$y>20)) continue; $bit=$bits[$i%strlen($bits)]??'0'; $mix=(ord($payload[$i%$len]??'A')+$x*7+$y*11+$i)%5; if($bit==='1'||$mix===0) $svg.='<rect x="'.($x*$cell).'" y="'.($y*$cell).'" width="'.$cell.'" height="'.$cell.'" fill="#111"/>'; $i++; }} return $svg.'</svg>'; }
    public static function productPayload(array $p): string { return json_encode(['product_id'=>$p['id']??null,'sku'=>$p['sku']??'','url'=>\url('product/'.($p['slug']??'')),'price'=>$p['sale_price']??$p['regular_price']??0,'vendor'=>$p['shop_name']??$p['vendor_id']??'','stock'=>$p['stock']??0], JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES); }
}
