<?php

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

class AIEO_Courier_Driver_Custom extends AIEO_Courier_Driver_Base {

    const PREFIX = 'custom__';

    
    private $slug;
    
    private $label;
    
    private $tracking_url_template;

    public function __construct($slug, $label, $tracking_url_template = '') {
        $this->slug                  = sanitize_key((string) $slug);
        $this->label                 = (string) $label;
        $this->tracking_url_template = (string) $tracking_url_template;
    }

    public function code()        { return self::PREFIX . $this->slug; }
    public function label()       { return $this->label !== '' ? $this->label : strtoupper($this->slug); }
    public function capabilities() { return array('create'); }
    public function create_mode() { return 'manual'; }

    public function tracking_url($voucher_no) {
        if ($this->tracking_url_template === '') { return ''; }
        $vno = trim((string) $voucher_no);
        if ($vno === '') { return ''; }
        
        
        
        $url = $this->tracking_url_template;
        $url = str_replace(array('{voucher_no}', '{tracking_no}', '{number}'), rawurlencode($vno), $url);
        if (strpos($url, '%s') !== false) { $url = sprintf($url, rawurlencode($vno)); }
        return $url;
    }

    
    public static function slugify_user_input($raw) {
        $slug = sanitize_key((string) $raw);
        if ($slug === '') { return ''; }
        
        
        $reserved = array('acs', 'geniki', 'boxnow', 'elta', 'manual', 'speedex');
        if (in_array($slug, $reserved, true)) { return ''; }
        return $slug;
    }
}
