<?php

namespace traits;

use classes\bird;

trait bird_route_helpers_t
{
    private function normalizeOptionalString(mixed $value): string
    {
        if (!is_scalar($value)) {
            return '';
        }
        $normalized = trim((string)$value);
        if ($normalized === '') {
            return '';
        }
        $lower = strtolower($normalized);
        if ($lower === 'undefined' || $lower === 'null') {
            return '';
        }
        return $normalized;
    }

    private function getConfiguredWorkspaceId(bird $client): string
    {
        $workspaceConfig = null;
        if (property_exists($client->config, 'workspaceId')) {
            $workspaceConfig = $client->config->workspaceId;
        } elseif (property_exists($client->config, 'workplaceId')) {
            // Backward compatibility with existing config key naming.
            $workspaceConfig = $client->config->workplaceId;
        }
        if (is_object($workspaceConfig) && method_exists($workspaceConfig, 'getVariableValue')) {
            return $this->normalizeOptionalString($workspaceConfig->getVariableValue());
        }
        return '';
    }

    private function getConfiguredChannelId(bird $client): string
    {
        $channelConfig = null;
        if (property_exists($client->config, 'channelId')) {
            $channelConfig = $client->config->channelId;
        }
        if (is_object($channelConfig) && method_exists($channelConfig, 'getVariableValue')) {
            return $this->normalizeOptionalString($channelConfig->getVariableValue());
        }
        return '';
    }
}
