/**
 * Audace — Full-height option for core/columns and core/cover.
 *
 * Adds an `audaceFullHeight` boolean attribute and a toggle in the block
 * inspector. When enabled, a `has-audace-full-height` class is added; CSS
 * computes 100vh minus the site header and the admin bar.
 */
(function (wp) {
    'use strict';
    if (!wp || !wp.hooks) return;

    var addFilter        = wp.hooks.addFilter;
    var __               = (wp.i18n && wp.i18n.__) ? wp.i18n.__ : function (s) { return s; };
    var createHigherOrderComponent = wp.compose.createHigherOrderComponent;
    var Fragment         = wp.element.Fragment;
    var createElement    = wp.element.createElement;
    var InspectorControls = wp.blockEditor.InspectorControls;
    var ToggleControl    = wp.components.ToggleControl;

    var TARGETS = ['core/columns', 'core/cover'];
    var ATTR    = 'audaceFullHeight';
    var CLASS   = 'has-audace-full-height';

    function isTarget(name) { return TARGETS.indexOf(name) !== -1; }

    addFilter('blocks.registerBlockType', 'audace/full-height/attribute', function (settings, name) {
        if (!isTarget(name)) return settings;
        if (!settings.attributes) settings.attributes = {};
        settings.attributes[ATTR] = settings.attributes[ATTR] || { type: 'boolean', default: false };
        return settings;
    });

    var withControl = createHigherOrderComponent(function (BlockEdit) {
        return function (props) {
            if (!isTarget(props.name)) return createElement(BlockEdit, props);
            var on = !!props.attributes[ATTR];
            return createElement(
                Fragment,
                null,
                createElement(BlockEdit, props),
                createElement(
                    InspectorControls,
                    null,
                    createElement('div', { style: { padding: '16px' } },
                        createElement(ToggleControl, {
                            label: __('Pleine hauteur d\'écran', 'audace'),
                            help: __('Le bloc occupe 100vh moins le header et la barre d\'admin.', 'audace'),
                            checked: on,
                            onChange: function (v) {
                                var u = {};
                                u[ATTR] = !!v;
                                props.setAttributes(u);
                            }
                        })
                    )
                )
            );
        };
    }, 'withAudaceFullHeight');
    addFilter('editor.BlockEdit', 'audace/full-height/control', withControl);

    addFilter('blocks.getSaveContent.extraProps', 'audace/full-height/save-class', function (extra, blockType, attrs) {
        if (isTarget(blockType.name) && attrs && attrs[ATTR]) {
            extra.className = (extra.className ? extra.className + ' ' : '') + CLASS;
        }
        return extra;
    });

    addFilter('editor.BlockListBlock', 'audace/full-height/editor-class', createHigherOrderComponent(function (BlockListBlock) {
        return function (props) {
            if (!isTarget(props.name) || !props.attributes || !props.attributes[ATTR]) {
                return createElement(BlockListBlock, props);
            }
            var newProps = Object.assign({}, props, {
                className: (props.className ? props.className + ' ' : '') + CLASS
            });
            return createElement(BlockListBlock, newProps);
        };
    }, 'withAudaceFullHeightEditorClass'));

})(window.wp);
