# Copyright (c) 2025 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com

from decimal import Decimal
from typing import Any

from snowflake.connector.converter import SnowflakeConverter


def _canonical_identifier(identifier: str, preserve_case: bool = False) -> str:
    if not preserve_case:
        identifier = identifier.upper()
    return identifier.replace('"', '""')


def quoted_identifier(*identifiers: str, preserve_case: bool = False) -> str:
    return '.'.join(f'"{_canonical_identifier(i, preserve_case=preserve_case)}"' for i in identifiers)


def quoted_literal(literal: Any) -> str:
    adjusted_literal = str(literal) if isinstance(literal, Decimal) else literal
    return SnowflakeConverter.quote(SnowflakeConverter.escape(adjusted_literal))


def identifier_literal(identifier: str, prefix: bool = False) -> str:
    return SnowflakeConverter.quote(_canonical_identifier(f'{identifier}{"%" if prefix else ""}'))
