import React from "react";
import styled from "styled-components";
import ReactMde from "react-mde";
import { WidgetProps } from "react-jsonschema-form";
import MarkdownStyles from "@views/common/MarkdownStyles";

type MarkdownEditorWidgetProps = Omit<WidgetProps, "options"> & {
  options: unknown;
};

const Container = styled.div`
  background: white;
  color: black;

  button {
    color: black;
  }

  .mde-preview {
    min-height: unset !important;
    height: 200px;
    overflow: scroll;
  }
`;

const MarkdownEditorWidget: React.FC<MarkdownEditorWidgetProps> = ({ value, onChange }) => {
  const [selectedTab, setSelectedTab] = React.useState<"write" | "preview">("write");
  return (
    <Container>
      <ReactMde
        value={value}
        onChange={onChange}
        selectedTab={selectedTab}
        onTabChange={setSelectedTab}
        generateMarkdownPreview={(markdown) => Promise.resolve(
          <MarkdownStyles>{markdown}</MarkdownStyles>,
        )}
      />
    </Container>
  );
};

export default MarkdownEditorWidget;
