import React, { useMemo, useState } from "react";
import PropTypes from "prop-types";
import { Box, Typography } from "@strapi/design-system";
import CKEReact from "../../../components/CKEReact.jsx";
import EditorGlobalStyles from "../../../components/EditorGlobalStyles.jsx";
import { hydratePreset } from "../../../presets/hydrate";

const SAMPLE_CONTENT = `
<h2>Preview</h2>
<p>This is a <strong>live preview</strong> of the preset. Try the toolbar above.</p>
<ul><li>First item</li><li>Second item</li></ul>
`;

const PreviewTab = ({ draft }) => {
  const [value, setValue] = useState(SAMPLE_CONTENT);

  const previewKey = useMemo(() => JSON.stringify(draft), [draft]);
  const preset = useMemo(() => hydratePreset(draft), [previewKey]);

  return (
    <Box>
      <Typography variant="pi" textColor="neutral500">
        Changes in other tabs re-create this editor
      </Typography>
      <Box paddingTop={2} className="ckstudio-editor">
        <EditorGlobalStyles />
        <CKEReact
          key={previewKey}
          preset={preset}
          value={value}
          onChange={setValue}
        />
      </Box>
    </Box>
  );
};

PreviewTab.propTypes = {
  draft: PropTypes.object.isRequired,
};

export default PreviewTab;
