import { Button } from "antd";
import { JsonEditor as Editor } from "jsoneditor-react";
import "jsoneditor-react/es/editor.min.css";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { connect } from "react-redux";
import { createStructuredSelector } from "reselect";

const mapStateToProps = createStructuredSelector({});
const mapDispatchToProps = () => ({
  //setUserLanguage: language => dispatch(setUserLanguage(language))
});

export function ShopTemplateTestRender({ emailEditorRef, style }) {
  const [variables, setVariables] = useState({ id: "uuid" });
  const [loading, setLoading] = useState(false);
  const { t } = useTranslation();

  const handleTestRender = async () => {
    try {
      setLoading(true);

      emailEditorRef.current.exportHtml(async () => {
        try {
          setLoading(false);
        } catch (error) {
          setLoading(false);
          alert(error);
        }
      });
    } catch (error) {
      setLoading(false);
      alert(error);
    }
  };

  return (
    <div style={{ ...style, margin: "1rem", display: "flex" }}>
      <div style={{ flex: 1 }}>
        <Editor value={variables} onChange={(e) => setVariables(e)} />
      </div>
      <Button loading={loading} type="ghost" onClick={handleTestRender}>
        {t("bodyshop.actions.testrender")}
      </Button>
    </div>
  );
}

export default connect(mapStateToProps, mapDispatchToProps)(ShopTemplateTestRender);
