import React from 'react';
import Input from '../input';
import './style';
import { jsx as _jsx } from "react/jsx-runtime";
const languageFormat = (text, language) => {
  if (typeof text !== 'string') {
    return text;
  }
  try {
    if (language === 'json') {
      return JSON.stringify(JSON.parse(text), null, 2);
    }
  } catch (error) {
    // console.log(error)
  }
  return text;
};

/**
 * 代码片段组件 这个组件为了显示简单的配置，复杂的请使用更加重型的组件
 *
 * @param
 */
const FieldCode = ({
  text,
  mode,
  render,
  language = 'text',
  renderFormItem,
  plain,
  fieldProps
}, ref) => {
  const code = languageFormat(text, language);
  if (mode === 'read') {
    const dom = /*#__PURE__*/_jsx("pre", {
      ref: ref,
      ...fieldProps,
      style: {
        padding: 16,
        overflow: 'auto',
        fontSize: '85%',
        lineHeight: 1.45,
        backgroundColor: '#f6f8fa',
        borderRadius: 3,
        width: 'min-content',
        ...fieldProps.style
      },
      children: /*#__PURE__*/_jsx("code", {
        children: code
      })
    });
    if (render) {
      return render(code, {
        mode,
        ...fieldProps,
        ref
      }, dom);
    }
    return dom;
  }
  if (mode === 'edit' || mode === 'update') {
    let dom = /*#__PURE__*/_jsx(Input.TextArea, {
      rows: 5,
      ...fieldProps,
      ref: ref
    });
    if (plain) {
      dom = /*#__PURE__*/_jsx(Input, {
        ...fieldProps,
        ref: ref
      });
    }
    if (renderFormItem) {
      return renderFormItem(code, {
        mode,
        ...fieldProps,
        ref
      }, dom);
    }
    return dom;
  }
  return null;
};
export default /*#__PURE__*/React.forwardRef(FieldCode);