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

/**
 * 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 { animationType, animationDuration, animationDelay, scrollBehavior } = attributes;

	// Get animation info text
	const getAnimationInfo = () => {
		const duration = animationDuration ? `${animationDuration}ms` : '300ms';
		const delay = animationDelay ? ` + ${animationDelay}ms delay` : '';
		const behavior = scrollBehavior === 'followScroll' ? 'Follow Scroll' : 'Play on View';
		return `${animationType} (${duration}${delay}) - ${behavior}`;
	};

	const blockProps = useBlockProps();

	return (
		<div {...blockProps}>
			<div className="animation-content">
				<div className="animation-info">
					<svg xmlns="http://www.w3.org/2000/svg" width="20" viewBox="0 0 576 512">
						<path d="M150.6 489.4L128 512l-22.6-22.6L54.6 438.6 32 416l22.6-22.6L425.4 22.6 448 0l22.6 22.6 50.7 50.7L544 96l-22.6 22.6L150.6 489.4zM498.7 96L448 45.3 337.9 155.3l50.7 50.7L498.7 96zM128 466.7L366.1 228.7l-50.7-50.7L77.3 416 128 466.7z"/>
					</svg>
					<div className="animation-message">
						{__("Animation Block", "orphic-ui")}
						<span>{getAnimationInfo()}</span>
					</div>
				</div>
				<InnerBlocks />
			</div>
			<SidePanel attributes={attributes} setAttributes={setAttributes} />
		</div>
	);
}
