jQuery(document).ready(function ($) {
    function updateEditTemplateButton() {
        const $select = $("select[data-setting='template_id']");
        const $button = $("#sitsel-edit-template-button");

        if ($select.length) {
            const id = $select.val();
            if (id) {
                $button.attr("href", sitSelEditor.editUrlBase + id).show();
            } else {
                $button.hide();
            }
        }
    }

    // Update when template changes
    $(document).on("change", "select[data-setting='template_id']", updateEditTemplateButton);

    // Initialize when editor loads
    setTimeout(updateEditTemplateButton, 800);
});
