<?php
declare(strict_types=1);

if (!function_exists('elms_setting')) {
    function elms_setting(mysqli $db, string $key, string $default = ''): string
    {
        static $cache = null;

        if ($cache === null) {
            $cache = [];

            if ($res = $db->query("SHOW TABLES LIKE 'lms_settings'")) {
                if ($res->num_rows > 0) {
                    $q = $db->query("SELECT setting_key, setting_value FROM lms_settings");
                    if ($q) {
                        while ($r = $q->fetch_assoc()) {
                            $cache[(string)$r['setting_key']] = (string)$r['setting_value'];
                        }
                    }
                }
            }
        }

        return $cache[$key] ?? $default;
    }
}

if (!function_exists('elms_setting_on')) {
    function elms_setting_on(mysqli $db, string $key, string $default = 'off'): bool
    {
        return strtolower(elms_setting($db, $key, $default)) === 'on';
    }
}