function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
import React, { useEffect, useRef, useState } from 'react';
import { IconFont } from '@zgfe/business-lib';
import { Input } from 'antd';
import './index.less';
// import { basicSetup } from 'codemirror';
// import { syntaxTree } from "@codemirror/language"
import { sql } from '@codemirror/lang-sql';
import { EditorState, EditorSelection } from '@codemirror/state';
import { linter } from '@codemirror/lint';
import { EditorView } from '@codemirror/view';
import { sqlLint } from '../../utils';
import _ from 'lodash';
var sqlLang = sql();
var ExpressionColumnList;
var Expression = function Expression(_ref) {
  var columnList = _ref.columnList,
    expression = _ref.expression,
    setExpression = _ref.setExpression,
    errMsg = _ref.errMsg,
    setErrMsg = _ref.setErrMsg;
  var classPrefix = 'expression-box';
  var editorRef = useRef(null);
  window.ExpressionColumnList = columnList;
  ExpressionColumnList = columnList;
  var _useState = useState(),
    _useState2 = _slicedToArray(_useState, 2),
    editor = _useState2[0],
    setEditor = _useState2[1];
  var _useState3 = useState({
      from: -1,
      to: 0
    }),
    _useState4 = _slicedToArray(_useState3, 2),
    selection = _useState4[0],
    setSelection = _useState4[1];
  var _useState5 = useState(expression),
    _useState6 = _slicedToArray(_useState5, 2),
    doc = _useState6[0],
    setDock = _useState6[1];
  var _useState7 = useState(''),
    _useState8 = _slicedToArray(_useState7, 2),
    selectData = _useState8[0],
    setSelectData = _useState8[1];
  var _useState9 = useState(columnList),
    _useState0 = _slicedToArray(_useState9, 2),
    option = _useState0[0],
    setoption = _useState0[1];
  useEffect(function () {
    setoption(columnList.filter(function (item) {
      return item.name.indexOf(selectData) !== -1 ? true : false;
    }));
  }, [selectData, columnList]);
  var regexpLinter = linter(function (view) {
    var diagnostics = [];
    var err = sqlLint(view.state.doc.toString().replace(/\t|\n|\v|\r|\f/g, ''), ExpressionColumnList);
    setErrMsg(err ? err : '');
    if (err) {
      setExpression('');
      diagnostics.push({
        from: 0,
        to: view.state.doc.length,
        severity: 'error',
        message: err
      });
    } else {
      setExpression(view.state.doc.toString());
    }
    return diagnostics;
  });
  useEffect(function () {
    // 初始化CodeMirror编辑器
    var state = EditorState.create({
      doc: doc,
      extensions: [sqlLang, regexpLinter, EditorView.updateListener.of(function (v) {
        setSelection(v.state.selection.ranges[0]);
        if (v.docChanged) {
          setDock(v.state.doc.toString());
        }
      })]
    });
    var editor = new EditorView({
      state: state,
      parent: editorRef.current
    });
    setEditor(editor);
    return function () {
      editor.destroy(); // 注意：此后此处要随组件销毁
    };
  }, []);
  var onClickFun = function onClickFun(type, insert) {
    var focusLength = selection.to + insert.length - (type === 'fun' ? 1 : 0);
    editor === null || editor === void 0 ? void 0 : editor.dispatch({
      changes: _objectSpread(_objectSpread({}, selection), {}, {
        insert: insert
      }),
      selection: EditorSelection.range(focusLength, focusLength)
    });
    editor === null || editor === void 0 ? void 0 : editor.focus();
  };
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-title")
  }, "\u51FD\u6570"), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
    onClick: function onClick() {
      return onClickFun('fun', 'AVG()');
    }
  }, "AVG"), /*#__PURE__*/React.createElement("div", {
    onClick: function onClick() {
      return onClickFun('fun', 'COUNT()');
    }
  }, "COUNT"), /*#__PURE__*/React.createElement("div", {
    onClick: function onClick() {
      return onClickFun('fun', 'SUM()');
    }
  }, "SUM"))), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-title")
  }, "\u9009\u62E9\u5B57\u6BB5"), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement(Input, {
    placeholder: "\u8BF7\u8F93\u5165\u5185\u5BB9",
    prefix: /*#__PURE__*/React.createElement(IconFont, {
      type: "sousuo"
    }),
    onChange: function onChange(e) {
      return setSelectData(_.trim(e.currentTarget.value));
    }
  })), /*#__PURE__*/React.createElement("div", null, option.map(function (item) {
    return /*#__PURE__*/React.createElement("div", {
      key: item.name,
      onClick: function onClick() {
        return onClickFun('field', item.name);
      }
    }, item.name);
  }))), /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-title")
  }, "\u7F16\u8F91\u8868\u8FBE\u5F0F"), errMsg && (/*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-errmsg")
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "tishi"
  }), errMsg)), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-editor-box"),
    ref: editorRef
  })));
};
export default Expression;