/**
 * Retrieves the translation of text.
 *
 * @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-i18n/
 */
import { __ } from '@wordpress/i18n';


/**
 * Use Effect Hook
 */
import { useEffect } from '@wordpress/element';

/**
 * React hook that is used to mark the block wrapper element.
 * It provides all the necessary props like the class name.
 *
 * @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops
 */
import { useBlockProps, InnerBlocks } from '@wordpress/block-editor';

/**
 * Lets webpack process CSS, SASS or SCSS files referenced in JavaScript files.
 * Those files can contain any CSS code that gets applied to the editor.
 *
 * @see https://www.npmjs.com/package/@wordpress/scripts#using-css
 */
import './editor.scss';
import SidePanel from './side-panel';

/**
 * The edit function describes the structure of your block in the context of the
 * editor. This represents what the editor will render when the block is used.
 *
 * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit
 *
 * @return {Element} Element to render.
 */

;


export default function Edit({ attributes, setAttributes }) {
  const template = [
    ['core/paragraph', {  placeholder: __('Votre contenu ici.', 'orphic-block-and-function') }]
  ]
  
  // Ajout de la gestion des couleurs du thème
  const colors = window.OrphicWPColorsTheme;

  useEffect(() => {
    if (colors && colors.length > 0 && attributes.isFirstTimeChangeColor) {
      console.log(colors);
      const primaryColor = '#FFFFFF';
      const backgroundColor = colors.find(c => c.slug === "background")?.color || 'green';
      const textColor = colors.find(c => c.slug === 'text')?.color || '#172B4D';
      const borderColor = colors.find(c => c.slug === 'border')?.color || 'transparent';
      const errorBg = colors.find(c => c.slug === 'error-bg')?.color || '#ffbdad';
      const errorText = colors.find(c => c.slug === 'error-text')?.color || '#de350b';
      const primaryBg = colors.find(c => c.slug === 'primary-bg')?.color || '#0065FF';

      setAttributes({
        backgroundColor,
        textColor,
        borderColor,
        primaryColor,
        primaryBg,
        errorBg,
        errorText
      });
      setAttributes({ isFirstTimeChangeColor: false });
    }
  }, []);

  const isCustomColor = attributes.isCustomColor;

  let style = {};
  if (isCustomColor) {
    style = {
      "--orphic-ui-persistent-banner-background-color": attributes.backgroundColor,
      "--orphic-ui-persistent-banner-text-color": attributes.textColor,
      "--orphic-ui-persistent-banner-border-color": attributes.borderColor,
      "--orphic-ui-persistent-banner-primary-color": attributes.primaryColor,
      "--orphic-ui-persistent-banner-primary-bg": attributes.primaryBg,
      "--orphic-ui-persistent-banner-error-bg": attributes.errorBg,
      "--orphic-ui-persistent-banner-error-text": attributes.errorText,
    };
  }
  
  return (
    <div {...useBlockProps()}>
      <SidePanel attributes={attributes} setAttributes={setAttributes} />
      <div className={`orphic-ui-persistent-banner-content ${isCustomColor ? 'customColor' : ''}`} 
           style={style}>
        <div className="orphic-ui-persistent-banner-wrapper">
          <div className="orphic-ui-persistent-banner-text">
            <InnerBlocks template={template} />
          </div>
          <div className="orphic-ui-persistent-banner-buttons">
          <button className="orphic-ui-persistent-banner-close">
              {attributes.closeText || 'Close'}
            </button>
            {attributes.isAgreeEnabled && (
              <button className="orphic-ui-persistent-banner-agree">
                {attributes.agreeText || 'Agree'}
              </button>
            )}
          </div>
        </div>
      </div>
    </div>
  );
}
