import { defineCellType } from '../../types';
import { ObjectView } from './View';
import { ObjectEdit } from './Edit';
import { safeStringify, safeParse } from '@wix/patterns-fields';
export { ObjectView, ObjectEdit };
export const objectCellType = defineCellType({
    type: 'object',
    ViewComponent: ObjectView,
    EditComponent: ObjectEdit,
    serialize: (value) => (value != null ? safeStringify(value) : ''),
    deserialize: (raw) => {
        if (!raw) {
            return {};
        }
        const parsed = safeParse(raw);
        return typeof parsed === 'object' &&
            parsed !== null &&
            !Array.isArray(parsed)
            ? parsed
            : {};
    },
    validate: (value) => {
        if (value == null) {
            return null;
        }
        if (typeof value === 'string') {
            try {
                const parsed = JSON.parse(value);
                if (typeof parsed !== 'object' ||
                    parsed === null ||
                    Array.isArray(parsed)) {
                    return 'Value must be a valid JSON object';
                }
            }
            catch {
                return 'Invalid JSON syntax';
            }
        }
        if (typeof value !== 'object' || Array.isArray(value)) {
            return 'Value must be a valid JSON object';
        }
        return null;
    },
    editable: true,
    clearable: true,
});
//# sourceMappingURL=index.js.map