( function( wp, sifency_editor_data ) {
    if ( ! wp || ! wp.data || ! sifency_editor_data ) {
        return;
    }

    wp.domReady( function() {
        if ( ! wp.data.select( 'core/editor' ).isCleanNewPost() ) {
            return;
        }

        const typeInput = document.getElementById( 'sifency-initial-template-type' );
        if ( ! typeInput ) {
            return;
        }

        const typeSlug = typeInput.value;
        const termsMap = sifency_editor_data.terms_map || {};

        if ( typeSlug && termsMap.hasOwnProperty( typeSlug ) ) {
            const termId = termsMap[typeSlug];
            if ( termId > 0 ) {
                const { editPost } = wp.data.dispatch( 'core/editor' );

                editPost( {
                    sifency_builder_type: [ termId ]
                } );

                editPost( {
                    meta: { '_sifency_initial_type': typeSlug }
                } );

                typeInput.remove();
            }
        }
    } );

} )( window.wp, window.sifency_editor_data );