/**
 * Audace — Document sidebar panel for the Production CPT.
 * Lets the editor pick an attachment; stores its ID in `_audace_download_file`.
 */
(function (wp) {
    'use strict';
    if (!wp || !wp.plugins || !wp.editor) return;

    var __                         = wp.i18n.__;
    var registerPlugin             = wp.plugins.registerPlugin;
    var PluginDocumentSettingPanel = (wp.editor && wp.editor.PluginDocumentSettingPanel) || (wp.editPost && wp.editPost.PluginDocumentSettingPanel);
    var Button                     = wp.components.Button;
    var MediaUpload                = wp.blockEditor.MediaUpload;
    var MediaUploadCheck           = wp.blockEditor.MediaUploadCheck;
    var useSelect                  = wp.data.useSelect;
    var useDispatch                = wp.data.useDispatch;
    var el                         = wp.element.createElement;
    var Fragment                   = wp.element.Fragment;

    if (!PluginDocumentSettingPanel || !MediaUpload) return;

    function Panel() {
        var postType = useSelect(function (select) {
            return select('core/editor').getCurrentPostType();
        }, []);
        if (postType !== 'production') return null;

        var meta = useSelect(function (select) {
            return select('core/editor').getEditedPostAttribute('meta') || {};
        }, []);
        var editPost = useDispatch('core/editor').editPost;

        var fileId = parseInt(meta._audace_download_file, 10) || 0;

        var attachment = useSelect(function (select) {
            if (!fileId) return null;
            return select('core').getEntityRecord('postType', 'attachment', fileId) || null;
        }, [fileId]);

        var title = attachment && (attachment.title && attachment.title.rendered || attachment.source_url) || '';

        function setId(id) { editPost({ meta: { _audace_download_file: id ? parseInt(id, 10) : 0 } }); }

        return el(
            PluginDocumentSettingPanel,
            { name: 'audace-production-file', title: __('Fichier à télécharger', 'audace'), className: 'audace-production-file-panel' },
            el(MediaUploadCheck, null,
                el(MediaUpload, {
                    onSelect: function (media) { setId(media && media.id); },
                    value: fileId,
                    render: function (obj) {
                        return el(Fragment, null,
                            fileId
                                ? el('p', { style: { marginTop: 0, wordBreak: 'break-all' } }, title || ('#' + fileId))
                                : el('p', { style: { marginTop: 0, opacity: 0.7 } }, __('Aucun fichier sélectionné.', 'audace')),
                            el(Button, { variant: 'secondary', onClick: obj.open },
                                fileId ? __('Remplacer', 'audace') : __('Choisir un fichier', 'audace')
                            ),
                            fileId ? ' ' : null,
                            fileId ? el(Button, { variant: 'tertiary', isDestructive: true, onClick: function () { setId(0); } }, __('Retirer', 'audace')) : null
                        );
                    }
                })
            )
        );
    }

    registerPlugin('audace-production-file', { render: Panel });
})(window.wp);
