<?php

namespace classes;

class Helpers
{
    /**
     * Parses the filter parameters string into an array by replacing specific
     * characters to emulate a JSON format and then splitting the string.
     *
     * @param string $filterParams The filter parameters in a string format
     *                             where parameters are separated by '&' and
     *                             key-value pairs are separated by '='.
     * @return array An array of parsed filter parameters.
     */
    public static function parseFilter(string $filterParams): array
    {
        // Replace Chars in Input String to emulate a JSON format instead of GET format
        $parsedFilterStr = str_replace('=', ':', $filterParams);
        $parsedFilterStr = str_replace('&', ',', $parsedFilterStr);
        $parsedFilterStr = str_replace('|AMPERSAND|', '&', $parsedFilterStr);

        // Explode Input String Into Array - Separated by ","
        return explode(',', $parsedFilterStr);
    }

    public static function createReferenceCode(string $prefix, int $initialValue, int $length = 6): string
    {
        return $prefix . str_pad($initialValue, $length, '0', STR_PAD_LEFT);
    }

    public static function debug($data): void
    {
        $debug_backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
        $filename = basename($debug_backtrace[0]['file']);
        echo "Debug at $filename - {$debug_backtrace[1]['function']} - line {$debug_backtrace[0]['line']}" . PHP_EOL;
        print_r($data);
        exit;
    }
}