{% load schema_editor_tags %}

<script>
    $(document).ready(function() {
        // CSRF Token setup
        var csrftoken = Cookies.get('csrftoken');
        {#        var csrftoken = '{{ csrf_token }}';#}
        if (csrftoken != null) {
            // Adapted from Django DOCS
            function csrfSafeMethod(method) {
                // these HTTP methods do not require CSRF protection
                return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
            }

            $.ajaxSetup({
                beforeSend: function(xhr, settings) {
                    if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
                        xhr.setRequestHeader("X-CSRFToken", csrftoken);
                    }
                }
            });
        }

        var update_callback = function (message_block) {
            var post_data = {
                messages: JSON.stringify(message_block.messages)
            };

            $.ajax({
                type: 'POST',
                url: "{{update_url}}",
                data: post_data,
                success: function (reply) {
                    let responses = reply.responses;
                    let response_block = {
                        id: message_block.id,
                        responses: responses
                    };
                    sch_ed.notify_responses(response_block);
                },
                dataType: 'json'
            });
        };

        var sch_ed = new schema_editor.SchemaEditor(
            {{ schema | safe }},
            update_callback,
        );
    });
</script>

{{ schema_editor_vue_templates_html | safe }}
