import { jsx, jsxs } from "react/jsx-runtime";
import { ErrorText, Label, LocaleBadge, useFieldError, useFieldValue } from "@byline/ui/react";
import classnames from "classnames";
import { defaultEditorConfig } from "./field/config/default.js";
import { EditorField } from "./field/editor-field.js";
import richtext_field_module from "./richtext-field.module.js";
const RichTextField = ({ field, value, defaultValue, editorConfig, readonly = false, instanceKey, onChange, path, locale })=>{
    const fieldPath = path ?? field.name;
    const fieldError = useFieldError(fieldPath);
    const fieldValue = useFieldValue(fieldPath);
    const incomingValue = value ?? fieldValue;
    const incomingDefault = defaultValue;
    const fieldId = instanceKey ? `${field.name}-${instanceKey}` : field.name;
    const baseEditorConfig = field.editorConfig ?? editorConfig ?? defaultEditorConfig;
    const resolvedEditorConfig = void 0 === field.embedRelationsOnSave ? baseEditorConfig : {
        ...baseEditorConfig,
        settings: {
            ...baseEditorConfig.settings,
            embedRelationsOnSave: field.embedRelationsOnSave
        }
    };
    const labelNode = locale && field.label ? /*#__PURE__*/ jsxs("div", {
        className: classnames('byline-field-richtext-label', richtext_field_module["label-row"]),
        children: [
            /*#__PURE__*/ jsx(Label, {
                id: `${fieldId}-label`,
                label: field.label,
                htmlFor: fieldId,
                required: !field.optional
            }),
            /*#__PURE__*/ jsx(LocaleBadge, {
                locale: locale
            })
        ]
    }) : field.label;
    return /*#__PURE__*/ jsx("div", {
        className: classnames('byline-field-richtext', field.name, richtext_field_module.wrapper),
        children: /*#__PURE__*/ jsxs("div", {
            className: classnames('byline-field-richtext-body', richtext_field_module.body),
            children: [
                /*#__PURE__*/ jsx(EditorField, {
                    onChange: onChange,
                    editorConfig: resolvedEditorConfig,
                    id: fieldId,
                    name: field.name,
                    description: field.helpText,
                    readonly: readonly,
                    label: labelNode,
                    required: !field.optional,
                    value: incomingValue,
                    defaultValue: incomingDefault
                }, fieldId),
                fieldError && /*#__PURE__*/ jsx(ErrorText, {
                    id: `${field.name}-error`,
                    text: fieldError
                })
            ]
        })
    });
};
export { RichTextField };
