import { useEffect, useState } from "react";
import { Button } from "../button";
import { GridArea, ResizeableGrid, useGridState } from "./resizeable-grid";
export default {
    title: "Component/Grid",
};
export let Default = {
    render: () => {
        return (<div style={{ height: "100%", padding: 16 }}>
				<ResizeableGrid persist="story" defaultValue={{
                columns: [200, "1fr", { size: 200, minSize: 100, maxSize: 300 }],
                rows: ["auto", "auto"],
            }} dragHandleSize="0.5rem">
					<GridArea column={{ start: 1, end: 2 }} row={{ start: 1, end: 2 }} style={{
                border: "1px solid black",
                height: "100%",
            }}>
						<h3>Column 1</h3>
					</GridArea>

					<GridArea column={{ start: 2, end: 3 }} row={{ start: 1, end: 2 }} style={{
                border: "1px solid black",
                height: "100%",
            }}>
						<h3>Column 2</h3>
					</GridArea>

					<GridArea column={{ start: 3, end: 4 }} row={{ start: 1, end: 2 }} style={{
                border: "1px solid black",
                height: "100%",
            }}>
						<h3>Column 3</h3>
					</GridArea>

					<GridArea column={{ start: 2, end: 4 }} row={{ start: 2, end: 3 }} style={{
                border: "1px solid black",
                height: "100%",
            }}>
						<h3>Row 2</h3>
					</GridArea>
				</ResizeableGrid>
			</div>);
    },
};
export let Controlled = {
    render: () => {
        let [state, setState] = useGridState({
            defaultColumns: [{ size: 200, mode: "static" }, "1fr", 200],
            defaultRows: ["auto", 300]
        });
        let toggleCol = (colIndex) => {
            setState(current => {
                let columns = current.columns.map((entry, index) => {
                    if (index === colIndex) {
                        return {
                            ...entry,
                            size: entry.size === 0 ? 200 : 0,
                        };
                    }
                    return entry;
                });
                return {
                    ...current,
                    columns,
                };
            });
        };
        let toggleRow = () => {
            setState(current => {
                let rows = current.rows.map((entry, index) => {
                    if (index === 1) {
                        return {
                            ...entry,
                            size: entry.size === 0 ? 200 : 0,
                        };
                    }
                    return entry;
                });
                return {
                    ...current,
                    rows,
                };
            });
        };
        let [count, setCount] = useState(0);
        useEffect(() => {
            setInterval(() => {
                setCount(c => c + 1);
            }, 100);
        }, []);
        return (<div style={{ display: "flex", flexDirection: "column", height: "100%", padding: 16 }}>
				<header style={{ display: "flex", justifyContent: "space-between" }}>
					<Button onClick={() => toggleCol(0)}>Toggle Left</Button>
					<Button onClick={() => toggleRow()}>Toggle Row</Button>
					<Button onClick={() => toggleCol(2)}>Toggle Right</Button>
				</header>
				<div style={{ height: "100%" }}>
					<ResizeableGrid persist="story" value={state} onChange={setState} dragHandleSize="0.5rem">
						<GridArea column={{ start: 1, end: 2 }} row={{ start: 1, end: 3 }} style={{
                border: "1px solid black",
                height: "100%",
                overflow: "hidden",
            }}>
							<h3>Column 1</h3>
						</GridArea>

						<GridArea column={{ start: 2, end: 3 }} row={{ start: 1, end: 2 }} style={{
                border: "1px solid black",
                height: "100%",
            }}>
							<h3>Column 2</h3>
							<p>{count}</p>
						</GridArea>
						<GridArea column={{ start: 3, end: 4 }} row={{ start: 1, end: 2 }} style={{
                border: "1px solid black",
                height: "100%",
                overflow: "hidden",
            }}>
							<h3>Column 3</h3>
						</GridArea>

						<GridArea column={{ start: 2, end: 4 }} row={{ start: 2, end: 3 }} style={{
                border: "1px solid black",
                height: "100%",
                overflow: "hidden",
            }}>
							<h3>Bottom Row</h3>
						</GridArea>
					</ResizeableGrid>
				</div>
			</div>);
    },
};
