#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from typing import Self, Any, Optional
from Interfaces.FormatModuleInterface import FormatModuleInterface
from Utils.Check import Check
from Utils.Patterns import RE

class CapitalizeFormat:
    
    def __init__(self:Self, 
        format:FormatModuleInterface, 
        options:Optional[dict[str, Any|None]] = None
    ) -> None:
        self.format:FormatModuleInterface = format

    def get(self:Self, 
        i:int, 
        inputs:str|list[Any|None], 
        shared:dict[str, Any|None] = {}, 
        fragments:list[str] = []
    ) -> str:
        return RE.CAPITAL_CASE.sub(lambda matches:(
            matches.group(1).upper()
        ), self.format.execute(i, (
            inputs if Check.is_string(inputs) else 
            inputs[0] if Check.is_string(inputs[0]) else 
        ""), shared, fragments))
    
    def check(self:Self, 
        i:int, 
        string:str, 
        inputs:str|list[Any|None], 
        shared:dict[str, Any|None] = {}, 
        fragments:list[str] = []
    ) -> int:
        
        substring:str = self.format.set_fragments_level((
            inputs if Check.is_string(inputs) else 
            inputs[0] if Check.is_string(inputs[0]) else 
        ""), fragments)
        has_variables:bool = RE.FRAGMENT_VARIABLES.search(substring) is not None

        return (
            self.format.get_check_length(i, string, substring, shared, fragments, False) if has_variables else 
        self.format.prepare_result(string, substring, shared))