/* eslint-disable spire/export-styles */
import Zone from "@insite/client-framework/Components/Zone";
import WidgetModule from "@insite/client-framework/Types/WidgetModule";
import WidgetProps from "@insite/client-framework/Types/WidgetProps";
import GridContainer from "@insite/mobius/GridContainer";
import GridItem from "@insite/mobius/GridItem";
import * as cssLinter from "css";
import * as React from "react";
import { css } from "styled-components";

const enum fields {
    rowCount = "rowCount",
    gap = "gap",
    isFullWidth = "isFullWidth",
    customCSS = "customCSS",
}

interface Props extends WidgetProps {
    fields: {
        [fields.rowCount]: number;
        [fields.gap]: number;
        [fields.isFullWidth]: boolean;
        [fields.customCSS]: string;
    };
}

// A widget that makes a comfigurable number of row zones within its container.
const Rows = ({ fields: { rowCount, gap, isFullWidth, customCSS }, id }: Props) => {
    const rows: JSX.Element[] = [];
    for (let row = 0; row < rowCount; row = row + 1) {
        rows.push(
            <GridItem key={`GridItem-${id}-${row}`} width={[12, 12, 12, 12, 12]}>
                <Zone contentId={id} zoneName={`Content${row}`} />
            </GridItem>,
        );
    }

    const gridCss = css`
        ${customCSS}
    `;

    return (
        <GridContainer css={gridCss} gap={gap} isFullWidth={isFullWidth}>
            {rows}
        </GridContainer>
    );
};

const defaultCustomCss = `& {

}`;

const advancedTab = {
    displayName: "Advanced",
    sortOrder: 1,
};

const widgetModule: WidgetModule = {
    component: Rows,
    definition: {
        group: "Basic",
        icon: "distribute-spacing-vertical",
        fieldDefinitions: [
            {
                name: fields.rowCount,
                displayName: "Row Count",
                editorTemplate: "IntegerField",
                defaultValue: 4,
                fieldType: "General",
                sortOrder: 1,
            },
            {
                name: fields.gap,
                displayName: "Gap Between Items",
                editorTemplate: "IntegerField",
                defaultValue: 0,
                fieldType: "General",
                sortOrder: 2,
            },
            {
                name: fields.isFullWidth,
                displayName: "Full Width",
                editorTemplate: "CheckboxField",
                defaultValue: false,
                fieldType: "General",
                isVisible: (_item, _advancedFeaturesEnabled, theme) => !!theme?.content?.isFullWidth,
                sortOrder: 3,
            },
            {
                name: "customCSS",
                displayName: "Custom CSS",
                editorTemplate: "CodeField",
                fieldType: "General",
                tab: advancedTab,
                defaultValue: defaultCustomCss,
                isVisible: (item, shouldDisplayAdvancedFeatures) => !!shouldDisplayAdvancedFeatures,
                validate: value => {
                    if (value === undefined || value === null) {
                        return "";
                    }

                    const result = cssLinter.parse(value, { silent: true });
                    if (result?.stylesheet?.parsingErrors) {
                        // the error output at this time only has room for one line so we just show the first error
                        return result.stylesheet.parsingErrors.length <= 0
                            ? ""
                            : result.stylesheet.parsingErrors.map(error => `${error.reason} on line ${error.line}`)[0];
                    }

                    return "Unable to parse Css";
                },
                options: {
                    mode: "css",
                    lint: true,
                    autoRefresh: true,
                },
            },
        ],
    },
};

export default widgetModule;
