from __future__ import annotations

import datetime as _dt


def left_zero(value: object, size: int) -> str:
    s = str(value)
    return s.zfill(size)


def to_aaaammdd(dt: _dt.date | _dt.datetime) -> str:
    if isinstance(dt, _dt.datetime):
        d = dt.date()
    else:
        d = dt
    return f"{d.year}{left_zero(d.month, 2)}{left_zero(d.day, 2)}"


def to_json_datetime(dt: _dt.datetime) -> str:
    # Formato estándar SIFEN para dFecFirma, dFecEm, dFecRe: AAAA-MM-DDThh:mm:ss (sin offset)
    return (
        f"{dt.year}-{left_zero(dt.month, 2)}-{left_zero(dt.day, 2)}T"
        f"{left_zero(dt.hour, 2)}:{left_zero(dt.minute, 2)}:{left_zero(dt.second, 2)}"
    )


def to_iso_datetime_no_offset(dt: _dt.datetime) -> str:
    return (
        f"{dt.year}-{left_zero(dt.month, 2)}-{left_zero(dt.day, 2)}T"
        f"{left_zero(dt.hour, 2)}:{left_zero(dt.minute, 2)}:{left_zero(dt.second, 2)}"
    )


def normalize_xml(xml: str) -> str:
    xml = xml.replace("\r\n", "")
    xml = xml.replace("\n", "")
    xml = xml.replace("\t", "")
    xml = xml.replace("    ", "")
    xml = xml.replace(">    <", "><")
    xml = xml.replace(">  <", "><")
    xml = xml.replace("\r", "")
    return xml
