import { Fragment as _Fragment, jsxs as _jsxs, jsx as _jsx } from "react/jsx-runtime";
import { TableContainer, Table, TableHead, TableCell, TableBody, TableRow, Paper } from "@material-ui/core";
import React, { useState } from "react";
import styled from "styled-components";
import { XDataType } from "../../Common/Data/XDataType";
import { StyledLink } from "../BuildUI/Link";
import { DeleteGrayIcon } from "../Icon/DeleteGrayIcon";
import { EditModeBooleanValueEditor } from "./Boolean";
import { EditModeDateValueEditor } from "./Date";
import { FieldNameRender } from "./DesignUI";
import { EditModeNumberValueEditor } from "./Number";
import { EditModeStringValueEditor } from "./String";
const ObjectListValueRender = ({ forData, length = 15, values, }) => {
    let requiredLength = false;
    let renderValue = "";
    let v = values[0] || {};
    renderValue += forData.fields.reduce((s, current, idx) => {
        const fieldValue = typeof v === "object" ? v[current.name] : current.name;
        if (idx === 0)
            return `${fieldValue}`;
        if (`${s}, ${fieldValue}`.length > length) {
            requiredLength = true;
            return s;
        }
        s = `${s}, ${fieldValue}`;
        return s;
    }, "");
    return (_jsxs(_Fragment, { children: [renderValue, " ", requiredLength ? `... (${values.length})` : ``] }, void 0));
};
export const DisplayModeObjectValueEditor = ({ value, forData }) => {
    return (_jsx("div", Object.assign({ className: "ValueEditor-display", "data-testid": "ValueEditor-Object-Display" }, { children: Array.isArray(value) ? (_jsx(ObjectListValueRender, { forData: forData, values: value }, void 0)) : (_jsx(_Fragment, { children: (forData.fields || []).reduce((s, current, idx) => {
                const fieldValue = value && typeof value === "object" ? Reflect.get(value || {}, current.name) : null;
                let renderValue = "";
                if (current.list) {
                    renderValue =
                        fieldValue && Array.isArray(fieldValue) && fieldValue.length
                            ? `${typeof fieldValue[0] === "object" ? current.name : fieldValue[0]}(${fieldValue.length})`
                            : `(0)`;
                }
                else {
                    renderValue = `${typeof fieldValue === "object" ? current.name : fieldValue || ""}`;
                }
                if (idx === 0) {
                    return renderValue;
                }
                return `${s}, ${renderValue}`;
            }, "") }, void 0)) }), void 0));
};
export const EditModeObjectValueEditor = React.forwardRef(({ value, forData, onUpdate, editMode, showFieldName = true }, ref) => {
    if (forData.list) {
        return (_jsx(ObjectListEditor, { values: value, updateCallback: onUpdate, forData: forData, showFieldName: showFieldName }, void 0));
    }
    let values = forData.list
        ? Array.isArray(value) && typeof value !== "string"
            ? value
            : value
                ? [value]
                : []
        : Array.isArray(value) && typeof value !== "string"
            ? value[0]
            : value;
    const onFieldValueUpdate = (newValue, fieldName) => {
        const originalValue = typeof values === "object" ? Object.assign({}, values) : {};
        let newValues = Object.assign(Object.assign({}, originalValue), { [fieldName]: newValue });
        onUpdate(newValues);
    };
    return (_jsx("div", Object.assign({ className: "ValueEditor-display", "data-testid": "ValueEditor-Object-Edit" }, { children: _jsx(_Fragment, { children: (forData.fields || []).map((field, idx) => (_jsxs(SubfieldEditorRender, { children: [showFieldName && _jsx(FieldNameRender, { children: field.name }, void 0),
                    _jsx(SubfieldEditor, { forData: field, value: values && typeof values === "object" ? Reflect.get(values || {}, field.name) : null, showFieldName: showFieldName, onUpdate: (newValue) => {
                            onFieldValueUpdate(newValue, field.name);
                        }, editMode: editMode }, idx)] }, idx))) }, void 0) }), void 0));
});
export const ObjectListEditor = (props) => {
    let values = [];
    if (Array.isArray(props.values)) {
        values = props.values;
    }
    const [viewDetails, setViewDetails] = useState({});
    const onUpdate = (newValue, fieldName, index) => {
        let newValues = [
            ...values.map((v, idx) => {
                if (index !== idx)
                    return v;
                return Object.assign(Object.assign({}, (typeof v === "object" ? v : {})), { [fieldName]: newValue });
            }),
        ];
        props.updateCallback([...newValues]);
    };
    const onViewDetails = (index) => {
        setViewDetails(Object.assign(Object.assign({}, viewDetails), { [`v_${index}`]: !viewDetails[`v_${index}`] }));
    };
    const add = () => {
        props.updateCallback([...values, {}]);
    };
    const remove = (index) => {
        let tempValues = [...values];
        tempValues.splice(index, 1);
        props.updateCallback([...tempValues]);
    };
    return (_jsx(StyledObjectList, Object.assign({ className: "PropertyList-Wrapper ObjectList-Wrapper", "data-testid": "ValueEditor-ObjectList-Edit" }, { children: _jsx(TableContainer, { children: _jsxs(Table, Object.assign({ size: "small", stickyHeader: true }, { children: [_jsx(TableHead, { children: _jsxs(TableRow, { children: [_jsx(TableCell, {}, void 0),
                                props.forData.fields.map((f, idx) => {
                                    return (_jsx(TableCell, { children: _jsx(FieldNameRender, Object.assign({ className: "headerColumn-title" }, { children: f.name }), void 0) }, idx));
                                }),
                                _jsx(TableCell, {}, void 0)] }, void 0) }, void 0),
                    _jsxs(TableBody, { children: [values.map((v, idx) => {
                                return (_jsxs(React.Fragment, { children: [_jsxs(TableRow, { children: [_jsx(TableCell, { children: idx + 1 }, void 0),
                                                props.forData.fields.map((field, idx2) => (_jsx(TableCell, { children: _jsx(SubfieldEditor, { forData: field, value: Reflect.get(v || {}, field.name), onUpdate: (newValue) => onUpdate(newValue, field.name, idx), editMode: "inline", subfield: true, onViewDetails: () => onViewDetails(idx), show: viewDetails[`v_${idx}`], showFieldName: props.showFieldName }, void 0) }, idx2))),
                                                _jsx(TableCell, Object.assign({ onClick: () => remove(idx), "data-testid": `BtnRemove${props.forData.name}` }, { children: _jsx(DeleteGrayIcon, {}, void 0) }), void 0)] }, void 0),
                                        viewDetails[`v_${idx}`] && (_jsx(TableRow, { children: _jsx(TableCell, Object.assign({ colSpan: props.forData.fields.length + 2, classes: {
                                                    root: "ObjectListTableCell-details",
                                                } }, { children: props.forData.fields
                                                    .filter((field) => field.type === XDataType.OBJECT)
                                                    .map((field, index) => (_jsxs(React.Fragment, { children: [_jsx(FieldNameRender, Object.assign({ className: "ValueEditor-fieldLabel" }, { children: field.name }), void 0),
                                                        _jsx(SubfieldEditor, { forData: field, value: Reflect.get(v || {}, field.name), onUpdate: (newValue) => onUpdate(newValue, field.name, idx), showFieldName: props.showFieldName, editMode: "inline" }, void 0)] }, index))) }), void 0) }, void 0))] }, idx));
                            }),
                            _jsx(TableRow, { children: _jsx(TableCell, Object.assign({ colSpan: props.forData.fields.length + 2 }, { children: _jsx(StyledLink, Object.assign({ onClick: add, color: "primary", classes: { root: "PropertyList-Add" }, "data-testid": `BtnAdd${props.forData.name}` }, { children: "Add" }), void 0) }), void 0) }, void 0)] }, void 0)] }), void 0) }, void 0) }), void 0));
};
const SubfieldEditor = React.forwardRef((props, ref) => {
    switch (props.forData.type) {
        case XDataType.BOOLEAN:
            return _jsx(EditModeBooleanValueEditor, Object.assign({}, props, { ref: ref }), void 0);
        case XDataType.STRING:
            return _jsx(EditModeStringValueEditor, Object.assign({}, props, { ref: ref }), void 0);
        case XDataType.DATE:
            return _jsx(EditModeDateValueEditor, Object.assign({}, props, { ref: ref }), void 0);
        case XDataType.NUMBER:
            return _jsx(EditModeNumberValueEditor, Object.assign({}, props, { ref: ref }), void 0);
        case XDataType.FILE:
            return null;
        case XDataType.OBJECT:
            return props.subfield ? (_jsx(ViewDetailsButton, Object.assign({ onClick: props.onViewDetails }, { children: !props.show ? "show details" : "hide details" }), void 0)) : (_jsx(EditModeObjectValueEditor, Object.assign({}, props, { ref: ref }), void 0));
        default:
            return null;
    }
});
const StyledObjectList = styled(Paper) `
    min-width: 600px;
    overflow: auto;

    text-align: right;
    margin: 20px 0;

    .ObjectListTableCell-details {
        padding: 20px;
    }

    .headerColumn-title {
        justify-content: center;
    }
`;
const ViewDetailsButton = styled.div `
    display: flex;
    align-items: center;
    justify-content: center;
`;
const SubfieldEditorRender = styled.div `
    padding: 5px 0;
`;
//# sourceMappingURL=Object.js.map