// Generates an image tag with src and srcset
// You can pass only the filename, the rest will be generated


prototype(GesagtGetan.Basic:ResourceImage) < prototype(Neos.Fusion:Tag) {
    packageKey = ${node.context.currentSite.siteResourcesPackageKey}
    basePath = ${'resource://' + this.packageKey + '/Public/Assets/'}

    filename = null

    array = ${String.split(this.filename, '.')}
    name = ${Array.join(Array.pop(this.array), '.')}
    extension = ${Array.last(this.array)}

    translationKey = ${String.firstLetterToLowerCase(String.replace(this.name, '.', '-'))}
    alt = ${Translation.translate(this.translationKey, null, [], null, this.packageKey)}
    class = ${String.toLowerCase(String.replace(this.name, '.', '-'))}
    srcset = ${Configuration.setting('GesagtGetan.Basic.ResourceImage.srcset')}

    @if.hasFilename = ${this.filename && this.name && this.extension}

    @context {
        alt = ${this.alt}
        class = ${this.class}
        basePath = ${this.basePath}
        name = ${this.name}
        extension = ${this.extension}
        srcset = ${this.srcset}
    }

    tagName = 'img'
    attributes {
        alt = ${alt}
        class = ${class}
        src = Neos.Fusion:ResourceUri {
            path = ${basePath + name + '.' + extension}
        }
        srcset = Neos.Fusion:Collection {
            collection = ${srcset}
            itemName = 'resolution'
            iterationName = 'iterator'
            itemRenderer = Neos.Fusion:ResourceUri {
                path = ${basePath + name + (resolution == 1 ? '.' : '@' + resolution + 'x.') + extension}
                @process {
                    // replaceWithAt is because of an Neos bug. @ get replaced with %402, so we convert it back to the old value
                    replaceWithAt = ${String.replace(value, '%40', '@')}
                    addResolution = ${value + ' ' + resolution + 'x'}
                    addComma = ${iterator.isLast ? value : value + ','}
                }
            }
            @if {
                isNotSVG = ${extension != 'svg'}
                srcset = ${srcset && Type.isArray(srcset) && Array.length(srcset)}
            }
        }
    }

}
