"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
exports.__esModule = true;
exports["default"] = void 0;
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
var _inheritsLoose2 = _interopRequireDefault(require("@babel/runtime/helpers/inheritsLoose"));
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
var _select = _interopRequireDefault(require("@alifd/next/lib/select"));
var _react = _interopRequireWildcard(require("react"));
var _locale = require("../../../src/locale");
var _coreConnectorBeta = require("@cverse/core-connector-beta");
var _coreLibBeta = require("@cverse/core-lib-beta");
var _utils = require("../../_utils/utils");
var _lodash = require("lodash");
var _boxSlotComponent = _interopRequireDefault(require("../_components/boxSlotComponent"));
var _type = require("./type");
var _renderByFormKey = _interopRequireDefault(require("../_components/renderByFormKey"));
var _formKeyType = require("../../_utils/formKeyType");
var _utils2 = require("../cBehavior-setter/config/utils");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
var Option = _select["default"].Option;
var _useWalletConnect = (0, _coreConnectorBeta.useWalletConnect)(),
  walletConnect = _useWalletConnect.walletConnect;
var webApi = new _coreLibBeta.WebApi();
var WebListOptions = [{
  label: (0, _locale.t)('setter.webSelectDefault'),
  value: _type.WebSelectType["default"]
}, {
  label: (0, _locale.t)('setter.webSelectCustom'),
  value: _type.WebSelectType.custom
}];
var CWebListSetter = function CWebListSetter(props) {
  var value = props.value,
    onChange = props.onChange;
  var _useState = (0, _react.useState)((value === null || value === void 0 ? void 0 : value.webType) || _type.WebSelectType["default"]),
    webType = _useState[0],
    setWebType = _useState[1];
  var _useState2 = (0, _react.useState)((value === null || value === void 0 ? void 0 : value.webGid) || ''),
    webGid = _useState2[0],
    setWebGid = _useState2[1];
  var _useState3 = (0, _react.useState)((0, _utils2.handleStringToRequest)((value === null || value === void 0 ? void 0 : value.webGidParams) || value)),
    webGidParams = _useState3[0],
    setWebGidParams = _useState3[1];
  var _useState4 = (0, _react.useState)([]),
    webList = _useState4[0],
    setWebList = _useState4[1];
  var currentWebGid = (0, _utils.getLocationParamsByName)("webGid");
  var _useState5 = (0, _react.useState)([]),
    telegramRelationOption = _useState5[0],
    setTelegramRelationOption = _useState5[1];
  //  请求当前项目下的网页
  (0, _react.useEffect)(function () {
    setTelegramRelationOption((0, _formKeyType.getRelationList)());
    var fetchData = /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2["default"])(/*#__PURE__*/_regenerator["default"].mark(function _callee() {
        var projectId, _webListRes$data, _webListRes$data$data, webListRes;
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) switch (_context.prev = _context.next) {
            case 0:
              projectId = (0, _utils.getLocationParamsByName)("pId");
              if (!projectId) {
                _context.next = 6;
                break;
              }
              _context.next = 4;
              return webApi.getWebList({
                projectId: (0, _lodash.toNumber)(projectId),
                page: 1,
                rows: 1000
              });
            case 4:
              webListRes = _context.sent;
              if (webListRes.code === 200 && (_webListRes$data = webListRes.data) !== null && _webListRes$data !== void 0 && (_webListRes$data$data = _webListRes$data.data) !== null && _webListRes$data$data !== void 0 && _webListRes$data$data.length) {
                setWebList(webListRes.data.data);
              }
            case 6:
            case "end":
              return _context.stop();
          }
        }, _callee);
      }));
      return function fetchData() {
        return _ref.apply(this, arguments);
      };
    }();
    fetchData();
  }, []);
  (0, _react.useEffect)(function () {
    var obj = {
      webType: webType,
      webGid: webGid,
      webGidParams: webGidParams
    };
    onChange(obj);
  }, [webType, webGid, webGidParams]);
  return /*#__PURE__*/_react["default"].createElement("div", null, /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.webType")
  }, /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: (0, _locale.t)("setter.selectPlaceHolder"),
    notFoundContent: (0, _locale.t)('setter.notFoundContent'),
    dataSource: WebListOptions,
    value: webType,
    onChange: function onChange(v) {
      setWebType(v);
    }
  })), /*#__PURE__*/_react["default"].createElement(_boxSlotComponent["default"], {
    title: (0, _locale.t)("setter.webOrigin")
  }, webType === _type.WebSelectType["default"] && /*#__PURE__*/_react["default"].createElement(_select["default"], {
    className: "itemOne_select",
    style: {
      width: 100 + "%",
      minWidth: 0
    },
    placeholder: (0, _locale.t)("setter.webListSelectTip"),
    value: webGid,
    onChange: function onChange(gid) {
      setWebGid(gid);
    }
  }, webList.map(function (web) {
    if (web.gid !== currentWebGid) {
      return /*#__PURE__*/_react["default"].createElement(Option, {
        value: web.gid
      }, web.name);
    }
  })), webType === _type.WebSelectType.custom && /*#__PURE__*/_react["default"].createElement(_renderByFormKey["default"], {
    relationOption: telegramRelationOption,
    value: webGidParams,
    onChange: function onChange(v) {
      (0, _utils.handleRenderByFormKeyData)(v, function (newItem) {
        var newData = (0, _extends2["default"])({}, webGidParams, newItem);
        setWebGidParams(newData);
      });
    }
  })));
};
var _default = exports["default"] = /*#__PURE__*/function (_Component) {
  function _default() {
    return _Component.apply(this, arguments) || this;
  }
  (0, _inheritsLoose2["default"])(_default, _Component);
  var _proto = _default.prototype;
  _proto.render = function render() {
    return /*#__PURE__*/_react["default"].createElement(CWebListSetter, this.props);
  };
  return _default;
}(_react.Component);