from __future__ import annotations

import unicodedata

from clarabot_nano.common.config.extract.base import AbstractExtractedConfig
from clarabot_nano.utility.path.unicode import PLATFORM_UNICODE_NORMALIZATION


class PathUnicodeNormalizationExtractedConfig(AbstractExtractedConfig):
    def __init__(self, config):
        # NOTE: this is for convenience as it cannot change runtime (local).
        self.path_unicode_normalization = config["local"]["path_unicode_normalization"]
        if self.path_unicode_normalization == "default":
            self.effective_normalization = PLATFORM_UNICODE_NORMALIZATION
        elif self.path_unicode_normalization == "keep":
            self.effective_normalization = None
        else:
            self.effective_normalization = self.path_unicode_normalization

    def _matches(self, other: PathUnicodeNormalizationExtractedConfig) -> bool:
        return self.path_unicode_normalization == other.path_unicode_normalization

    def normalize_remote_path(self, path: str) -> str:
        """
        This shall only be used on paths that will be used as target for write operations.
        See docstring in clarabot_nano.utility.path.unicode for details.
        """
        if self.effective_normalization:
            return unicodedata.normalize(self.effective_normalization, path)
        return path
