/**
 * @package     DOCman
 * @copyright   Copyright (C) 2019 Timble CVBA. (http://www.timble.net)
 * @license     GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link        http://www.joomlatools.com
 */

(function(wp, editor, blockEditor, components, blocks, global)
{
    document.addEventListener("DOMContentLoaded", function()
    {
        var el = wp.element.createElement;

        var blockName = 'docman/flat';
        var config = FoliokitBlockConfigurations[blockName];

        Docman.Block.setConfig(config);

        config.edit = function(props) {

            var _ = Foliokit.translate;

            return [
                el(
                    blockEditor.InspectorControls,
                    null,
                    el(components.PanelBody, {
                        title: _('Category'),
                        initialOpen: true
                    }, [
                        Docman.Block.getControl('category', config.attributes.category, props),
                        Docman.Block.getControl('category_children', config.attributes.category_children, props)
                    ]),
                    el(components.PanelBody, {
                        title: _('Layout'),
                        initialOpen: true
                    }, [
                        Docman.Block.getControl('layout', config.attributes.layout, props)
                    ]),
                    el(components.PanelBody, {
                        title: _('Tags'),
                        initialOpen: true
                    }, [
                        Docman.Block.getControl('tags', config.attributes.tags, props)
                    ]),
                    el(components.PanelBody, {
                        title: _('Document Options'),
                        initialOpen: false
                    }, [
                        Docman.Block.getControl('sort_documents', config.attributes.sort_documents, props),
                        Docman.Block.getControl('show_action_buttons', config.attributes.show_action_buttons, props),
						Docman.Block.getControl('show_pagination', config.attributes.show_pagination, props),
                        Docman.Block.getControl('documents_per_page', config.attributes.documents_per_page, props),
                        Docman.Block.getControl('show_document_search', config.attributes.show_document_search, props),
                        Docman.Block.getControl('show_document_sort_limit', config.attributes.show_document_sort_limit, props),
                        Docman.Block.getControl('show_document_created', config.attributes.show_document_created, props),
                        Docman.Block.getControl('show_document_created_by', config.attributes.show_document_created_by, props),
                        Docman.Block.getControl('show_document_modified', config.attributes.show_document_modified, props),
                        Docman.Block.getControl('show_document_filename', config.attributes.show_document_filename, props),
                        Docman.Block.getControl('show_document_size', config.attributes.show_document_size, props),
                        Docman.Block.getControl('show_document_hits', config.attributes.show_document_hits, props),
                        Docman.Block.getControl('show_document_extension', config.attributes.show_document_extension, props),
                        Docman.Block.getControl('track_downloads', config.attributes.track_downloads, props),
                        Docman.Block.getControl('force_download', config.attributes.force_download, props),
                        Docman.Block.getControl('allow_multi_download', config.attributes.allow_multi_download, props),
                        Docman.Block.getControl('download_in_blank_page', config.attributes.download_in_blank_page, props),
                        Docman.Block.getControl('show_document_recent', config.attributes.show_document_recent, props),
                        Docman.Block.getControl('show_document_popular', config.attributes.show_document_popular, props),
                        Docman.Block.getControl('hits_for_popular', config.attributes.hits_for_popular, props),
                        Docman.Block.getControl('days_for_new', config.attributes.days_for_new, props),
                    ]),
                ),
                el(components.Placeholder, {
                    label: config.title,
                    icon: config.icon
                })
            ];
        };

        config.save = function()  {
            return null; //save has to exist. This all we need
        };

        config.icon = Docman.Block.icon;

        blocks.registerBlockType(blockName, config);

        global.Docman.Block.setNodeHandler('.flat_layout_selector', function(node) {
			Docman.Block.addHandler({values: ['list', 'table', 'gallery'], element: node.find('select'), handler: 'selectVisibility'});
        });

		global.Docman.Block.setNodeHandler('.show_pagination_control', function(node) {
			Docman.Block.addHandler({selector: '.k-visibility_pagination__show', attribute: 'show_pagination', element: node.find('input'), handler: 'toggleVisibility'});
        });
    });
})(wp, wp.editor, wp.blockEditor, wp.components, wp.blocks, window);
