"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _reactBlockly = _interopRequireDefault(require("react-blockly"));

var _blockly = _interopRequireDefault(require("blockly"));

var _python = _interopRequireDefault(require("blockly/python"));

var _reactUse = require("react-use");

require("../../blockly.css");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(source, true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(source).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

_blockly["default"].Python = _python["default"];
var BlocklyEditor = _reactBlockly["default"].BlocklyEditor;

var CodeEditor = function CodeEditor(_ref, ref) {
  var _ref$language = _ref.language,
      language = _ref$language === void 0 ? 'JavaScript' : _ref$language,
      _ref$height = _ref.height,
      height = _ref$height === void 0 ? '100%' : _ref$height,
      _ref$toolboxBlocks = _ref.toolboxBlocks,
      toolboxBlocks = _ref$toolboxBlocks === void 0 ? [] : _ref$toolboxBlocks,
      _ref$workspaceConfigu = _ref.workspaceConfiguration,
      workspaceConfiguration = _ref$workspaceConfigu === void 0 ? {
    grid: {
      spacing: 20,
      length: 3,
      colour: '#ccc',
      snap: true
    },
    scrollbars: true,
    sounds: true,
    trashcan: true
  } : _ref$workspaceConfigu,
      _ref$onCodeChange = _ref.onCodeChange,
      onCodeChange = _ref$onCodeChange === void 0 ? console.log : _ref$onCodeChange,
      _ref$extraBlockDefini = _ref.extraBlockDefinition,
      extraBlockDefinition = _ref$extraBlockDefini === void 0 ? function () {} : _ref$extraBlockDefini,
      _ref$workspaceXML = _ref.workspaceXML,
      workspaceXML = _ref$workspaceXML === void 0 ? '<xml></xml>' : _ref$workspaceXML,
      _ref$editorConfig = _ref.editorConfig,
      editorConfig = _ref$editorConfig === void 0 ? {} : _ref$editorConfig;

  var _useState = (0, _react.useState)(''),
      _useState2 = _slicedToArray(_useState, 2),
      code = _useState2[0],
      setcode = _useState2[1];

  var _useState3 = (0, _react.useState)(''),
      _useState4 = _slicedToArray(_useState3, 2),
      xml = _useState4[0],
      setxml = _useState4[1];

  var _useState5 = (0, _react.useState)(null),
      _useState6 = _slicedToArray(_useState5, 2),
      editor = _useState6[0],
      seteditor = _useState6[1];

  var editorRef = (0, _react.useCallback)(function (node) {
    if (node !== null) {
      seteditor(node);
    }
  }, []);
  (0, _react.useEffect)(function () {
    onCodeChange(code);
  }, [code]);
  (0, _react.useEffect)(function () {
    loadFromXML();
  }, [workspaceXML, editor]);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      getCode: function getCode() {
        return code;
      },
      getXML: function getXML() {
        return xml;
      }
    };
  });
  extraBlockDefinition(_blockly["default"]);

  function loadFromXML() {
    var _editor = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : editor;

    function htmlDecode(input) {
      var e = document.createElement('div');
      e.innerHTML = input;
      return e.childNodes[0].nodeValue;
    }

    if (_editor !== null) {
      var _xml2 = htmlDecode(workspaceXML);

      if (_xml2 === null) {
        _xml2 = workspaceXML;
      }

      if (!_editor.workspace) {
        return;
      }

      _editor.workspace.state.workspace.clear();

      _editor.workspace.importFromXml(_xml2);
    }
  }

  function handleWorkSpaceChange(workspace) {
    var _code = _blockly["default"][language].workspaceToCode(workspace);

    var _xml = _blockly["default"].Xml.domToText(_blockly["default"].Xml.workspaceToDom(workspace));

    setxml(_xml);
    setcode(_code);
  }

  return _react["default"].createElement("div", {
    style: {
      height: height
    }
  }, _react["default"].createElement(BlocklyEditor, {
    key: JSON.stringify(editorConfig),
    ref: editorRef,
    toolboxBlocks: toolboxBlocks,
    workspaceConfiguration: _objectSpread({}, workspaceConfiguration, {}, editorConfig),
    wrapperDivClassName: "blockly-fill-height",
    workspaceDidChange: handleWorkSpaceChange
  }));
};

var _default = (0, _react.forwardRef)(CodeEditor);

exports["default"] = _default;