function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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 { ajax, BizGlobalDataContext, IconFont } from '@zgfe/business-lib';
import { Button, Checkbox, Form, Input, Radio, Space } from 'antd';
import React, { createRef, useContext, useEffect, useState } from 'react';
import apis from "../apis";
import VisualContext from "./context";
import "./styles/createPanel.less";
import { EndpointType } from "./types";
var prefix = "visual-workbench-create-panel";
var PageSelector = function PageSelector(_ref) {
  var value = _ref.value,
    onChange = _ref.onChange;
  var _useContext = useContext(VisualContext),
    currentPage = _useContext.currentPage;
  var _useState = useState(value || 'current'),
    _useState2 = _slicedToArray(_useState, 2),
    scope = _useState2[0],
    setScope = _useState2[1];
  useEffect(function () {
    if (onChange) onChange(scope);
  }, [scope]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Radio.Group, {
    onChange: function onChange(event) {
      setScope(event.target.value);
    },
    defaultValue: scope
  }, /*#__PURE__*/React.createElement(Space, {
    size: 48
  }, /*#__PURE__*/React.createElement(Radio, {
    value: "current"
  }, "\u5F53\u524D\u9875\u9762"), /*#__PURE__*/React.createElement(Radio, {
    value: "all"
  }, "\u6240\u6709\u9875\u9762"))), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "-detail")
  }, currentPage));
};
var Rules = function Rules(_ref2) {
  var value = _ref2.value,
    onChange = _ref2.onChange;
  var _useContext2 = useContext(VisualContext),
    currentElement = _useContext2.currentElement,
    setShowSimilar = _useContext2.setShowSimilar;
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    option = _useState4[0],
    setOption = _useState4[1];
  useEffect(function () {
    if (value) setOption(Object.keys(value));
  }, []);
  useEffect(function () {
    if (setShowSimilar) setShowSimilar(!(option !== null && option !== void 0 && option.includes('index')));
    if (!onChange) return;
    var data = {
      id: currentElement.identification
    };
    if (option !== null && option !== void 0 && option.includes('index')) {
      data.index = currentElement === null || currentElement === void 0 ? void 0 : currentElement.zgSupViewIndex;
    }
    if (option !== null && option !== void 0 && option.includes('viewName')) {
      data.viewName = currentElement === null || currentElement === void 0 ? void 0 : currentElement.viewName;
    }
    onChange(data);
  }, [option]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Checkbox.Group, {
    defaultValue: option,
    onChange: setOption
  }, /*#__PURE__*/React.createElement(Space, {
    size: 48
  }, (currentElement === null || currentElement === void 0 ? void 0 : currentElement.viewName) && /*#__PURE__*/React.createElement(Checkbox, {
    value: "viewName"
  }, "\u5185\u5BB9\u662F", currentElement === null || currentElement === void 0 ? void 0 : currentElement.viewName), /*#__PURE__*/React.createElement(Checkbox, {
    value: "index"
  }, "\u987A\u5E8F\u4E3A\u7B2C[", currentElement === null || currentElement === void 0 ? void 0 : currentElement.zgSupViewIndex, "]\u4F4D"))), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "-detail")
  }, "\u73B0\u5728\u5B9A\u4E49\u7684\u662F ", /*#__PURE__*/React.createElement("span", null, "[\u6240\u5C5E\u9875\u9762]"), " \u4E2D\uFF0C\u5728\u5217\u8868\u987A\u5E8F\u4E3A\u7B2C ", /*#__PURE__*/React.createElement("span", null, "[1]"), ' ', "\u4F4D\u7684\u5143\u7D20\uFF0C\u82E5\u5143\u7D20\u6B21\u5E8F\u6539\u53D8\uFF0C\u5C31\u4E0D\u518D\u7EE7\u7EED\u7EDF\u8BA1\u4E86"));
};
var CreatePanel = function CreatePanel() {
  var formRef = /*#__PURE__*/createRef();
  var _useContext3 = useContext(BizGlobalDataContext),
    currentApp = _useContext3.currentApp;
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    loading = _useState6[0],
    setLoading = _useState6[1];
  var _useContext4 = useContext(VisualContext),
    currentElement = _useContext4.currentElement,
    setCurrentElement = _useContext4.setCurrentElement,
    currentPage = _useContext4.currentPage,
    setEventList = _useContext4.setEventList,
    editEvent = _useContext4.editEvent,
    setEditEvent = _useContext4.setEditEvent;
  function onSubmit(formData) {
    var data = {
      endpointType: EndpointType.ios,
      appKey: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appKey,
      eventName: formData.name,
      eventType: currentElement ? 'click' : 'pv',
      pagePath: currentPage,
      elementConditionMap: formData.rules
    };
    setLoading(true);
    ajax(apis.create, {
      method: 'post',
      data: data
    }).then(function (res) {
      if (!res) return;
      if (setEventList) {
        setEventList(function (list) {
          return [].concat(_toConsumableArray(list), [_objectSpread(_objectSpread({}, data), {}, {
            id: res.data
          })]);
        });
      }
      if (setCurrentElement) setCurrentElement(undefined);
      if (setEditEvent) setEditEvent(undefined);
    }).catch(function (err) {
      console.error('创建/编辑可视化事件失败', err);
    }).finally(function () {
      setLoading(false);
    });
  }
  function onSave() {
    var _formRef$current;
    (_formRef$current = formRef.current) === null || _formRef$current === void 0 ? void 0 : _formRef$current.submit();
  }
  var initialValues;
  if (editEvent) {
    var _editEvent$elementCon, _editEvent$elementCon2;
    initialValues = {
      name: editEvent.eventName,
      scope: editEvent.pagePath ? 'current' : 'all',
      rules: {
        index: (_editEvent$elementCon = editEvent.elementConditions) === null || _editEvent$elementCon === void 0 ? void 0 : _editEvent$elementCon.index,
        viewName: (_editEvent$elementCon2 = editEvent.elementConditions) === null || _editEvent$elementCon2 === void 0 ? void 0 : _editEvent$elementCon2.viewName
      }
    };
  }
  return /*#__PURE__*/React.createElement("div", {
    className: prefix
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "-title")
  }, editEvent ? '编辑事件' : '创建事件', /*#__PURE__*/React.createElement(IconFont, {
    type: "qingchu"
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "-content")
  }, /*#__PURE__*/React.createElement(Form, {
    layout: "vertical",
    onFinish: onSubmit,
    ref: formRef,
    initialValues: initialValues,
    autoComplete: "off"
  }, /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u4E8B\u4EF6\u540D\u79F0",
    name: "name",
    rules: [{
      required: true
    }]
  }, /*#__PURE__*/React.createElement(Input, {
    allowClear: true,
    placeholder: "\u8BF7\u8F93\u5165\u4E8B\u4EF6\u540D\u79F0",
    autoFocus: true
  })), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u6240\u5C5E\u9875\u9762",
    name: "scope"
  }, /*#__PURE__*/React.createElement(PageSelector, null)), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u9650\u5B9A\u6761\u4EF6",
    name: "rules"
  }, /*#__PURE__*/React.createElement(Rules, null)), /*#__PURE__*/React.createElement(Form.Item, {
    label: "\u6700\u8FD17\u5929\u5143\u7D20\u70B9\u51FB\u60C5\u51B5"
  }, "xxx"))), /*#__PURE__*/React.createElement("div", {
    className: "".concat(prefix, "-foot")
  }, /*#__PURE__*/React.createElement(Space, {
    size: 16
  }, /*#__PURE__*/React.createElement(Button, {
    onClick: function onClick() {
      if (setCurrentElement) setCurrentElement(undefined);
      if (setEditEvent) setEditEvent(undefined);
    }
  }, "\u53D6\u6D88"), /*#__PURE__*/React.createElement(Button, {
    type: "primary",
    onClick: onSave,
    loading: loading
  }, "\u4FDD\u5B58"))));
};
export default CreatePanel;