function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _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(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { BizGlobalDataContext, IconFont } from '@zgfe/business-lib';
import request from '@zgfe/business-lib/es/utils/ajax';
import { message } from 'antd';
import React, { useContext, useEffect, useState } from 'react';
import apis from "../apis";
import VisualContext from "./context";
import CreatePanel from "./createPanel";
import ListPanel from "./listPanel";
import Mobile from "./mobile";
import "./styles/index.less";
import { classPrefix, EndpointType, Platform } from "./types";
var VisualWorkbench = function VisualWorkbench(_ref) {
  var platform = _ref.platform,
    socketUrl = _ref.socketUrl,
    onBack = _ref.onBack;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    eventList = _useState2[0],
    setEventList = _useState2[1];
  var _useContext = useContext(BizGlobalDataContext),
    currentApp = _useContext.currentApp;
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    currentPage = _useState4[0],
    setCurrentPage = _useState4[1];
  var _useState5 = useState(),
    _useState6 = _slicedToArray(_useState5, 2),
    currentElement = _useState6[0],
    setCurrentElement = _useState6[1];
  var _useState7 = useState({}),
    _useState8 = _slicedToArray(_useState7, 2),
    similarRules = _useState8[0],
    setSimilarRules = _useState8[1];
  var _useState9 = useState(),
    _useState10 = _slicedToArray(_useState9, 2),
    editEvent = _useState10[0],
    setEditEvent = _useState10[1];
  var _useState11 = useState(),
    _useState12 = _slicedToArray(_useState11, 2),
    client = _useState12[0],
    setClient = _useState12[1];
  var _useState13 = useState(false),
    _useState14 = _slicedToArray(_useState13, 2),
    definePv = _useState14[0],
    setDefinePv = _useState14[1];
  var _useState15 = useState(),
    _useState16 = _slicedToArray(_useState15, 2),
    pageData = _useState16[0],
    setPageData = _useState16[1];
  var _useState17 = useState(false),
    _useState18 = _slicedToArray(_useState17, 2),
    createFormChanged = _useState18[0],
    setCreateFormChanged = _useState18[1];
  var _useState19 = useState('disconnected'),
    _useState20 = _slicedToArray(_useState19, 2),
    status = _useState20[0],
    setStatus = _useState20[1];
  var _message$useMessage = message.useMessage(),
    _message$useMessage2 = _slicedToArray(_message$useMessage, 2),
    messageApi = _message$useMessage2[0],
    contextHolder = _message$useMessage2[1];
  function queryList() {
    request(apis.queryList, {
      method: 'post',
      data: {
        appKey: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appKey,
        endpointType: platform === Platform.ios ? EndpointType.ios : EndpointType.android
      }
    }).then(function (res) {
      if (!res) return;
      var list = res.data;
      list.forEach(function (item) {
        try {
          item.elementConditions = JSON.parse(item.elementConditions);
        } catch (e) {}
      });
      setEventList(list);
    }).catch(function (err) {
      console.error('获取可视化事件列表失败', err);
    });
  }
  function onDefinePv(pv) {
    setDefinePv(true);
    setCurrentElement(undefined);
    setEditEvent(pv);
  }
  useEffect(function () {
    queryList();
  }, []);
  useEffect(function () {
    client === null || client === void 0 ? void 0 : client.emit('update');
  }, [eventList]);
  return /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix)
  }, contextHolder, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-top")
  }, /*#__PURE__*/React.createElement("span", {
    onClick: onBack
  }, /*#__PURE__*/React.createElement(IconFont, {
    type: "fanhuiicon"
  }), "\u8FD4\u56DE"), "\u5B9A\u4E49APP\u53EF\u89C6\u5316\u4E8B\u4EF6"), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-main")
  }, /*#__PURE__*/React.createElement(VisualContext.Provider, {
    value: {
      status: status,
      setStatus: setStatus,
      platform: platform,
      socketUrl: socketUrl,
      currentElement: currentElement,
      setCurrentElement: setCurrentElement,
      eventList: eventList,
      setEventList: setEventList,
      currentPage: currentPage,
      setCurrentPage: setCurrentPage,
      similarRules: similarRules,
      setSimilarRules: setSimilarRules,
      editEvent: editEvent,
      setEditEvent: setEditEvent,
      client: client,
      createFormChanged: createFormChanged,
      setCreateFormChanged: setCreateFormChanged,
      pageData: pageData,
      setPageData: setPageData,
      initClient: function initClient(client) {
        setClient(client);
      }
    }
  }, /*#__PURE__*/React.createElement(ListPanel, null), /*#__PURE__*/React.createElement(Mobile, {
    onDefinePv: onDefinePv
  }), (currentElement || definePv) && /*#__PURE__*/React.createElement(CreatePanel, {
    key: "".concat(currentElement === null || currentElement === void 0 ? void 0 : currentElement.identification, "-").concat(currentElement === null || currentElement === void 0 ? void 0 : currentElement.zgSupViewIndex, "=").concat(definePv ? 'definePv' : ''),
    onSaved: function onSaved() {
      // 提示保存成功
      try {
        messageApi.success('保存成功');
      } catch (e) {}
    },
    onHide: function onHide() {
      setDefinePv(false);
      setCurrentElement(undefined);
    }
  }))));
};
export default VisualWorkbench;