( function ( wp ) {
	'use strict';

	if ( ! wp || ! wp.hooks ) {
		return;
	}

	var data = window.rsmsparkRsfEditor || {};
	var supported = data.supportedBlocks || [];
	var hoverBlocks = data.hoverBlocks || [];

	if ( ! supported.length ) {
		return;
	}

	function isSupportedBlock( name ) {
		if ( supported.indexOf( name ) !== -1 ) {
			return true;
		}

		if ( typeof name !== 'string' || '' === name ) {
			return false;
		}

		var prefixes = [
			'rsm-content-widgets/',
			'rsm-cw-compat/',
			'rsm/',
			'openrsm/',
		];

		for ( var i = 0; i < prefixes.length; i++ ) {
			if ( name.indexOf( prefixes[ i ] ) === 0 ) {
				return true;
			}
		}

		return name.indexOf( 'rsm-' ) === 0 && name.indexOf( '/' ) !== -1;
	}

	var hoverSchema = {
		rsmHoverTextColor: { type: 'string', default: '' },
		rsmHoverBackgroundColor: { type: 'string', default: '' },
		rsmHoverBorderColor: { type: 'string', default: '' },
	};

	var baseSchema = {
		rsmAnimation: { type: 'string', default: 'none' },
		rsmAnimationDuration: { type: 'number', default: 0 },
		rsmAnimationDelay: { type: 'number', default: 0 },
		rsmAnimationEasing: { type: 'string', default: '' },
		rsmAnimationStagger: { type: 'number', default: 0 },
		rsmHideMobile: { type: 'boolean', default: false },
		rsmHideTablet: { type: 'boolean', default: false },
		rsmHideDesktop: { type: 'boolean', default: false },
		rsmBand: { type: 'string', default: '' },
		rsmBandBg: { type: 'string', default: '' },
		rsmBandPadTop: { type: 'number', default: 0 },
		rsmBandPadBottom: { type: 'number', default: 0 },
		rsmSpacingTop: { type: 'number', default: 0 },
		rsmSpacingBottom: { type: 'number', default: 0 },
	};

	wp.hooks.addFilter(
		'blocks.registerBlockType',
		'rsm/rsf-attribute-schema',
		function ( settings, name ) {
			var schema = {};

			if ( isSupportedBlock( name ) ) {
				schema = Object.assign( {}, baseSchema );
			}

			if ( hoverBlocks.indexOf( name ) !== -1 ) {
				schema = Object.assign( schema, hoverSchema );
			}

			if ( ! Object.keys( schema ).length ) {
				return settings;
			}

			settings.attributes = Object.assign( {}, settings.attributes || {}, schema );

			return settings;
		}
	);
} )( window.wp );
