"use strict";

require("core-js/modules/es.object.keys.js");
require("core-js/modules/es.symbol.js");
require("core-js/modules/es.array.filter.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.object.get-own-property-descriptor.js");
require("core-js/modules/web.dom-collections.for-each.js");
require("core-js/modules/es.object.get-own-property-descriptors.js");
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("@wii/wau-react/lib/wau-modal/style");
var _wauModal = _interopRequireDefault(require("@wii/wau-react/lib/wau-modal"));
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
require("@wii/wau-react/lib/wau-message/style");
var _wauMessage2 = _interopRequireDefault(require("@wii/wau-react/lib/wau-message"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _react = require("react");
var _raven = _interopRequireDefault(require("@wii/raven"));
var _isEqual = _interopRequireDefault(require("lodash/isEqual"));
var _isNil = _interopRequireDefault(require("lodash/isNil"));
var _container = _interopRequireDefault(require("./container"));
var _jsxRuntime = require("react/jsx-runtime");
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) { (0, _defineProperty2.default)(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; }
/**
 * 默认类型
 */
var defaultCMSType = 'article';
function getModalOptions(options) {
  var _options$width = options.width,
    width = _options$width === void 0 ? 975 : _options$width,
    _options$height = options.height,
    height = _options$height === void 0 ? 708 : _options$height,
    _options$type = options.type,
    type = _options$type === void 0 ? defaultCMSType : _options$type,
    code = options.code,
    mode = options.mode;
  return {
    width: width,
    height: height,
    type: type,
    code: code,
    mode: mode
  };
}
function getCMSOptions(options) {
  var _options$type2 = options.type,
    type = _options$type2 === void 0 ? defaultCMSType : _options$type2,
    code = options.code,
    maxCount = options.maxCount,
    mode = options.mode;
  return {
    type: type,
    code: code,
    mode: mode,
    maxCount: maxCount
  };
}
function getRavenOptions(options) {
  var _options$url = options.url,
    url = _options$url === void 0 ? '' : _options$url,
    skey = options.skey,
    _options$type3 = options.type,
    type = _options$type3 === void 0 ? defaultCMSType : _options$type3,
    code = options.code,
    mode = options.mode;
  return {
    url: url,
    skey: skey,
    type: type,
    code: code,
    mode: mode
  };
}
var CMSModal = /*#__PURE__*/(0, _react.forwardRef)(function (_ref, ref) {
  var options = _ref.options,
    _ref$defaultVisible = _ref.defaultVisible,
    defaultVisible = _ref$defaultVisible === void 0 ? false : _ref$defaultVisible;
  var optionsRef = (0, _react.useRef)(options);
  var modalOptionsRef = (0, _react.useRef)(getModalOptions(options));
  var _useState = (0, _react.useState)(defaultVisible),
    _useState2 = (0, _slicedToArray2.default)(_useState, 2),
    visible = _useState2[0],
    setVisible = _useState2[1];
  var ravenRef = (0, _react.useRef)();
  var paramsRef = (0, _react.useRef)(getRavenOptions(options));
  var selectedRef = (0, _react.useRef)([]);
  var handleContainerMount = (0, _react.useCallback)(function (container) {
    var raven = new _raven.default({
      container: container,
      src: paramsRef.current.url,
      ignoreLayout: true,
      params: {
        header: 0,
        sider: 0,
        skey: paramsRef.current.skey,
        code: paramsRef.current.code,
        type: paramsRef.current.type,
        mode: paramsRef.current.mode
      }
    });
    ravenRef.current = raven;
    var changeEventId = raven.subscribe('change', function (_ref2) {
      var list = _ref2.list;
      selectedRef.current = list;
    });
    var toastEventId = raven.subscribe('toast', function (config) {
      if (config && typeof config.content !== 'string') {
        config.content = JSON.stringify(config.content);
      }
      _wauMessage2.default.open(config);
    });
    var paramsEventId = raven.subscribe('getParams', function () {
      return getCMSOptions(optionsRef.current);
    });
    var loginEventId = raven.subscribe('login', function () {
      var _optionsRef$current$o, _optionsRef$current;
      (_optionsRef$current$o = (_optionsRef$current = optionsRef.current).onAuth) === null || _optionsRef$current$o === void 0 ? void 0 : _optionsRef$current$o.call(_optionsRef$current);
    });
    return function () {
      raven.unsubscribe(toastEventId);
      raven.unsubscribe(changeEventId);
      raven.unsubscribe(paramsEventId);
      raven.unsubscribe(loginEventId);
      raven.destroy();
    };
  }, []);
  (0, _react.useImperativeHandle)(ref, function () {
    return {
      show: function show(opts) {
        var _ravenRef$current2;
        optionsRef.current = _objectSpread(_objectSpread({}, optionsRef.current), opts);
        var ravenParams = getRavenOptions(optionsRef.current);
        if (!(0, _isEqual.default)(paramsRef.current, ravenParams)) {
          var _ravenRef$current;
          paramsRef.current = _objectSpread({}, ravenParams);
          (_ravenRef$current = ravenRef.current) === null || _ravenRef$current === void 0 ? void 0 : _ravenRef$current.updateIframeConfig(ravenParams.url, {
            type: ravenParams.type,
            skey: ravenParams.skey,
            code: ravenParams.code,
            mode: ravenParams.mode
          });
        }
        (_ravenRef$current2 = ravenRef.current) === null || _ravenRef$current2 === void 0 ? void 0 : _ravenRef$current2.send('params', getCMSOptions(optionsRef.current));
        var modalOptions = getModalOptions(optionsRef.current);
        modalOptionsRef.current = modalOptions;
        setVisible(true);
      },
      hide: function hide() {
        return setVisible(false);
      }
    };
  });
  var handleCancelClick = (0, _react.useCallback)(function () {
    var _ravenRef$current3;
    setVisible(false);
    selectedRef.current = [];
    (_ravenRef$current3 = ravenRef.current) === null || _ravenRef$current3 === void 0 ? void 0 : _ravenRef$current3.send('clear');
  }, []);
  var handleOkClick = (0, _react.useCallback)(function () {
    if (!(0, _isNil.default)(optionsRef.current.maxCount) && selectedRef.current.length > optionsRef.current.maxCount) {
      _wauMessage2.default.error("\u6700\u591A\u80FD\u9009\u62E9 ".concat(optionsRef.current.maxCount, " \u9879\u5185\u5BB9"));
    } else {
      var _optionsRef$current2, _optionsRef$current2$, _ravenRef$current4;
      setVisible(false);
      (_optionsRef$current2 = optionsRef.current) === null || _optionsRef$current2 === void 0 ? void 0 : (_optionsRef$current2$ = _optionsRef$current2.callback) === null || _optionsRef$current2$ === void 0 ? void 0 : _optionsRef$current2$.call(_optionsRef$current2, {
        list: (0, _toConsumableArray2.default)(selectedRef.current),
        type: getModalOptions(optionsRef.current).type,
        code: getModalOptions(optionsRef.current).code
      });
      selectedRef.current = [];
      (_ravenRef$current4 = ravenRef.current) === null || _ravenRef$current4 === void 0 ? void 0 : _ravenRef$current4.send('clear');
    }
  }, []);
  return /*#__PURE__*/(0, _jsxRuntime.jsx)(_wauModal.default, _objectSpread(_objectSpread({
    title: "\u9009\u62E9\u5185\u5BB9",
    visible: visible,
    width: modalOptionsRef.current.width,
    bodyStyle: {
      padding: 0,
      height: modalOptionsRef.current.height
    },
    onCancel: handleCancelClick,
    onOk: handleOkClick,
    okText: "\u786E\u8BA4",
    cancelText: "\u53D6\u6D88"
  }, optionsRef.current.modal), {}, {
    children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_container.default, {
      onMount: handleContainerMount
    })
  }));
});
var _default = CMSModal;
exports.default = _default;