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

{% set additionalVars = {
    'isInherited': seomatic.helper.isInherited(metaGlobalVars, "canonicalUrl"),
    'fieldLabel': "Canonical URL"|t("seomatic"),
    'settingName': 'canonicalUrl'
} %}

{% embed "seomatic/settings/_includes/fields/_inheritableField.twig" with additionalVars %}
    {% macro canonicalUrlField(metaGlobalVars, suffix, pageContext) %}
        {% set disabled = suffix|length > 0 %}
        <div class="instructions">
            <p>
                {{ "The search engine friendly URL that you want the search engines to treat as authoritative."|t("seomatic") }}
                <a href="https://support.google.com/webmasters/answer/139066?hl=en"
                   target="_blank">{{ "Learn More"|t("seomatic") }}</a>
            </p>
        </div>

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

        {% namespace "metaGlobalVars" %}
            {% if pageContext != "field" %}
                {{ codeEditor.textField({
                    id: "canonicalUrl" ~ suffix,
                    name: "canonicalUrl",
                    value: metaGlobalVars.canonicalUrl,
                    warning: false,
                    errors: metaGlobalVars.getErrors("canonicalUrl"),
                }, "SeomaticExpressionField", {}, {wrapperClass: "monaco-editor-background-frame"}) }}
            {% else %}
                {% if not disabled %}
                    {{ forms.autosuggestField({
                        suggestEnvVars: true,
                        suggestAliases: true,
                        id: "canonicalUrl" ~ suffix,
                        name: "canonicalUrl",
                        value: metaGlobalVars.canonicalUrl,
                        class: "nicetext",
                        warning: false,
                        errors: metaGlobalVars.getErrors("canonicalUrl"),
                    }) }}
                {% else %}
                    {{ forms.textField({
                        id: "canonicalUrl" ~ suffix,
                        name: "canonicalUrl",
                        value: metaGlobalVars.canonicalUrl,
                        class: "nicetext",
                        warning: false,
                        disabled: true,
                        errors: metaGlobalVars.getErrors("canonicalUrl"),
                    }) }}
                {% endif %}
            {% endif %}
        {% endnamespace %}
    {% endmacro %}

    {% block inheritedValues %}
        {{ _self.canonicalUrlField(inheritedFrom.metaGlobalVars ?? metaGlobalVars, "-inherited", pageContext) }}
    {% endblock %}

    {% block field %}
        {{ _self.canonicalUrlField(metaGlobalVars, "", pageContext) }}
    {% endblock %}
{% endembed %}
