function clamp(value, minimum, maximum) {
    return Math.max(minimum, Math.min(maximum, value));
}

function finiteNumber(value, fallback) {
    var parsed = Number(value);
    return isFinite(parsed) ? parsed : fallback;
}

function iconFor(kind, ratio) {
    var name = String(kind || "").toLowerCase();
    if (name === "volume-muted")
        return "audio-volume-muted-symbolic";
    if (name === "volume") {
        if (ratio <= 0)
            return "audio-volume-muted-symbolic";
        if (ratio < 0.34)
            return "audio-volume-low-symbolic";
        if (ratio < 0.67)
            return "audio-volume-medium-symbolic";
        return "audio-volume-high-symbolic";
    }
    if (name === "microphone-muted")
        return "microphone-sensitivity-muted-symbolic";
    if (name === "microphone")
        return "audio-input-microphone-symbolic";
    if (name === "brightness")
        return "display-brightness-symbolic";
    if (name === "media-play" || name === "media-playing")
        return "media-playback-start-symbolic";
    if (name === "media-pause" || name === "media-paused")
        return "media-playback-pause-symbolic";
    if (name === "media-next")
        return "media-skip-forward-symbolic";
    if (name === "media-previous")
        return "media-skip-backward-symbolic";
    if (name === "media-stop")
        return "media-playback-stop-symbolic";
    return name || "dialog-information-symbolic";
}

function normalizedDuration(value) {
    var parsed = finiteNumber(value, 1400);
    return Math.max(0, Math.round(parsed));
}

function progressState(kind, rawValue, rawMaximum, rawLabel, rawDuration) {
    var maximum = Math.max(1, finiteNumber(rawMaximum, 100));
    var value = clamp(finiteNumber(rawValue, 0), 0, maximum);
    var ratio = value / maximum;
    var label = String(rawLabel || "");
    if (!label)
        label = Math.round(ratio * 100) + "%";

    return {
        kind: String(kind || ""),
        value: value,
        maximum: maximum,
        ratio: ratio,
        label: label,
        icon: iconFor(kind, ratio),
        duration: normalizedDuration(rawDuration),
        progress: true
    };
}

function messageState(kind, rawLabel, rawDuration) {
    return {
        kind: String(kind || ""),
        value: 0,
        maximum: 100,
        ratio: 0,
        label: String(rawLabel || ""),
        icon: iconFor(kind, 0),
        duration: normalizedDuration(rawDuration),
        progress: false
    };
}

if (typeof module !== "undefined") {
    module.exports = {
        clamp: clamp,
        iconFor: iconFor,
        progressState: progressState,
        messageState: messageState
    };
}
