"""Custom static files storage.

WhiteNoise's manifest storage post-processes JS/CSS during ``collectstatic`` and
strictly resolves every referenced file, including ``sourceMappingURL`` comments
in vendored bundles. Some third-party assets reference ``.map`` files that are
not shipped, which makes ``collectstatic`` fail hard.

``TolerantManifestStaticFilesStorage`` leaves such unresolved references
untouched instead of raising, so a missing source map can't break the build.
"""

from whitenoise.storage import CompressedManifestStaticFilesStorage


class TolerantManifestStaticFilesStorage(CompressedManifestStaticFilesStorage):
    # Don't 500 at runtime when a {% static %} reference isn't in the manifest;
    # fall back to the plain name (mirrors non-manifest storage behaviour).
    manifest_strict = False

    def _stored_name(self, name, hashed_files):
        """Tolerate missing references during collectstatic post-processing."""
        try:
            return super()._stored_name(name, hashed_files)
        except ValueError:
            return name

    def stored_name(self, name):
        """Tolerate missing manifest entries at request time."""
        try:
            return super().stored_name(name)
        except ValueError:
            return name
