import { mapKeys } from 'lodash-es';
import React from 'react';
import widgets from './widgets';
import { Typography } from '@material-ui/core';
/**
 * Convert a simple key/value properties object to an initialValues object with the prefixes used by widget components
 */
export function getFieldValuesForProperties(type, properties) {
    return mapKeys(properties, (value, key) => `${type}:${key}`);
}
/**
 * Convert a values object with the prefixes used by widget components into a simple key/value properties object
 */
export function getPropertiesForFieldValues(type, values) {
    const properties = {};
    for (const key in values) {
        if (key.startsWith(type + ':')) {
            properties[key.substr(type.length + 1)] = values[key];
        }
    }
    return properties;
}
/**
 * Single dynamic widget for a component property
 */
const PropertyWidget = props => {
    const { propertyType, property, initialData, disabled } = props;
    const { type } = property;
    if (type in widgets) {
        const WidgetPropertyComponent = widgets[type];
        return (React.createElement(WidgetPropertyComponent, { propertyType: propertyType, property: property, initialData: initialData, disabled: disabled }));
    }
    return React.createElement(Typography, { color: "error" }, "Unknown property type ", type);
};
/**
 * Shared component for outputting dynamic fields from properties table
 */
export const PropertyField = props => {
    const { propertyType, property, initialData, disabled } = props;
    return (React.createElement(PropertyWidget, { propertyType: propertyType, property: property, initialData: initialData, disabled: disabled }));
};
/**
 * Shared component for outputting dynamic fields from properties table
 */
export const PropertyFields = props => {
    const { propertyType, propertyTable, initialData } = props;
    return propertyTable.map(p => (React.createElement(PropertyWidget, { key: p.key, propertyType: propertyType, property: p, initialData: initialData })));
};
//# sourceMappingURL=PropertyFields.js.map