{% set inheritedFrom = seomatic.helper.findInheritableBundle(parentBundles, "seoImage") %}
{% set hasInheritableValues = inheritedFrom is not null %}

{% set additionalVars = {
    'isInherited': seomatic.helper.isInherited(metaGlobalVars, "seoImage"),
    'fieldLabel': "SEO Image Source"|t("seomatic"),
    'labelFor': 'metaBundleSettings-seoImageSource',
    'wrapperClass': 'seomatic-imageSourceInnerWrapper',
    'settingName': 'seoImage'
} %}

{% embed "seomatic/settings/_includes/fields/_inheritableField.twig" with additionalVars %}
    {% macro seoImageField(seoImageElements, elementType, assetVolumeSources, imageOptions, assetFieldSources, seomaticConfig, metaBundleSettings, metaGlobalVars, suffix, pageContext) %}
        {% set disabled = suffix|length > 0 %}
        <div class="instructions">
            <p>{{ "This is the image that will be used for display as the global website brand, as well as on X (Twitter) Cards and Facebook OpenGraph that link to the website, if they are not specified."|t("seomatic") }}</p>
        </div>

        {% import "_includes/forms" as forms %}
        {% import 'codeeditor/codeEditor' as codeEditor %}

        {% namespace "metaBundleSettings" %}
            <div class="heading">
                {{ forms.select({
                    id: "seoImageSource" ~ suffix,
                    name: "seoImageSource",
                    options: imageOptions,
                    value: metaBundleSettings.seoImageSource,
                    class: "seomatic-imageSourceSelect",
                    errors: metaBundleSettings.getErrors("seoImageSource"),
                    disabled: disabled,
                }) }}
            </div>
        {% endnamespace %}

        {% if assetFieldSources is defined and assetFieldSources |length %}
            {% namespace "metaBundleSettings" %}
                <div class="heading seomatic-imageSourceFromField">
                    <label
                        for="metaBundleSettings-seoImageField{{ suffix }}">{{ "Source Field: "|t("seomatic") }}</label>
                    {{ forms.select({
                        id: "seoImageField" ~ suffix,
                        name: "seoImageField",
                        options: assetFieldSources,
                        value: metaBundleSettings.seoImageField,
                        errors: metaBundleSettings.getErrors("seoImageField"),
                        disabled: disabled,
                    }) }}
                </div>
            {% endnamespace %}
        {% endif %}

        {% namespace "metaBundleSettings" %}
            <div class="heading seomatic-imageSourceFromAsset">
                {{ forms.elementSelect({
                    id: "seoImageIds" ~ suffix,
                    name: "seoImageIds",
                    viewMode: "large",
                    elements: seoImageElements,
                    elementType: elementType,
                    criteria: {
                        kind: ["image"],
                    },
                    jsClass: 'Craft.AssetSelectInput',
                    selectionLabel: "Select SEO Image"|t("seomatic"),
                    sources: assetVolumeSources,
                    limit: 1,
                    warning: false,
                    errors: metaBundleSettings.getErrors("seoImageIds"),
                    disabled: disabled,
                }) }}
            </div>
        {% endnamespace %}

        {% namespace "metaGlobalVars" %}
            <div class="input ltr seomatic-imageSourceFromUrl">
                {% if pageContext != "field" %}
                    {{ codeEditor.textField({
                        id: "seoImage" ~ suffix,
                        name: "seoImage",
                        value: metaGlobalVars.seoImage,
                        warning: false,
                        errors: metaGlobalVars.getErrors("seoImage"),
                        disabled: disabled,
                    }, "SeomaticExpressionField", {}, {wrapperClass: "monaco-editor-background-frame"}) }}
                {% else %}
                    {{ forms.text({
                        id: "seoImage" ~ suffix,
                        name: "seoImage",
                        value: metaGlobalVars.seoImage,
                        class: "nicetext",
                        warning: false,
                        errors: metaBundleSettings.getErrors("seoImage"),
                        disabled: disabled,
                    }) }}
                {% endif %}
            </div>
        {% endnamespace %}
    {% endmacro %}

    {% set imageOptions = {
        fromAsset: "Custom Image"|t("seomatic"),
        fromUrl: "Custom URL"|t("seomatic"),
    } %}

    {% if assetFieldSources is defined and assetFieldSources |length %}
        {% set imageOptions = { fromField: "From Asset Field"|t("seomatic"), } | merge(imageOptions) %}
    {% endif %}

    {% block inheritedValues %}
        {{ _self.seoImageField(seoImageElements, elementType, assetVolumeSources, imageOptions, assetFieldSources, seomatic.config, inheritedFrom.metaBundleSettings ?? metaBundleSettings, inheritedFrom.metaGlobalVars ?? metaGlobalVars, "-inherited", pageContext) }}
    {% endblock %}

    {% block field %}
        {{ _self.seoImageField(seoImageElements, elementType, assetVolumeSources, imageOptions, assetFieldSources, seomatic.config, metaBundleSettings, metaGlobalVars, "", pageContext) }}
    {% endblock %}
{% endembed %}

