document.addEventListener('DOMContentLoaded', function () {
    if (typeof tinymce === 'undefined') return;

    tinymce.init({
        selector: 'textarea.tinymce',
        plugins: [
          'anchor','autolink','charmap','codesample','emoticons','link','lists','media','searchreplace','table','visualblocks','wordcount',
          'checklist','mediaembed','casechange','formatpainter','pageembed','a11ychecker','tinymcespellchecker','permanentpen','powerpaste','advtable','advcode','advtemplate','tinymceai','uploadcare','mentions','tinycomments','tableofcontents','footnotes','mergetags','autocorrect','typography','inlinecss','markdown','importword','exportword','exportpdf'
        ],
        toolbar: 'undo redo | tinymceai-chat tinymceai-quickactions tinymceai-review | blocks fontfamily fontsize | bold italic underline strikethrough | link media table mergetags | addcomment showcomments | spellcheckdialog a11ycheck typography uploadcare | align lineheight | checklist numlist bullist indent outdent | emoticons charmap | removeformat',
        tinycomments_mode: 'embedded',
        tinycomments_author: 'Author name',
        mergetags_list: [
          { value: 'First.Name', title: 'First Name' },
          { value: 'Email', title: 'Email' }
        ],
        tinymceai_token_provider: async () => {
          await fetch('https://demo.api.tiny.cloud/1/lvnhsvfr4pq7am35w7milno0hbchkhp8xmrccl65vonf5hdq/auth/random', { method: "POST", credentials: "include" });
          return { token: await fetch('https://demo.api.tiny.cloud/1/lvnhsvfr4pq7am35w7milno0hbchkhp8xmrccl65vonf5hdq/jwt/tinymceai', { credentials: "include" }).then(r => r.text()) };
        },
        uploadcare_public_key: '5419d50771cb3750bec3',
        height: 320,
        branding: false,
        directionality: document.documentElement.getAttribute('dir') === 'rtl' ? 'rtl' : 'ltr',
        content_style: "body { font-family: Almarai, Tahoma, Arial, sans-serif; font-size: 14px; }"
    });
});
