"use strict";

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); }

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _react = _interopRequireWildcard(require("react"));

var _classnames = _interopRequireDefault(require("classnames"));

var _popup = _interopRequireDefault(require("../popup"));

var _button = _interopRequireDefault(require("../button"));

require("./index.css");

var _excluded = ["type", "width", "height", "time", "children", "popupPlacement", "classnames", "title", "desc", "step", "total", "isShowStep", "mask", "onClickClose", "buttonTitle", "visible", "innerBorderRadius", "displayType", "handleGoNextStep", "background", "innerStyle"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

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) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

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 = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

var preCls = 'titaui-pc__guide-order';
var GuideOrder = /*#__PURE__*/(0, _react.forwardRef)(function (props, ref) {
  var _props$type = props.type,
      type = _props$type === void 0 ? 'button' : _props$type,
      _props$width = props.width,
      width = _props$width === void 0 ? 280 : _props$width,
      height = props.height,
      _props$time = props.time,
      time = _props$time === void 0 ? 3000 : _props$time,
      children = props.children,
      _props$popupPlacement = props.popupPlacement,
      popupPlacement = _props$popupPlacement === void 0 ? 'top' : _props$popupPlacement,
      _props$classnames = props.classnames,
      classnames = _props$classnames === void 0 ? "".concat(preCls, "__popup") : _props$classnames,
      _props$title = props.title,
      title = _props$title === void 0 ? '标题' : _props$title,
      _props$desc = props.desc,
      desc = _props$desc === void 0 ? '描述' : _props$desc,
      _props$step = props.step,
      step = _props$step === void 0 ? '1' : _props$step,
      _props$total = props.total,
      total = _props$total === void 0 ? '5' : _props$total,
      _props$isShowStep = props.isShowStep,
      isShowStep = _props$isShowStep === void 0 ? true : _props$isShowStep,
      _props$mask = props.mask,
      mask = _props$mask === void 0 ? true : _props$mask,
      _props$onClickClose = props.onClickClose,
      onClickClose = _props$onClickClose === void 0 ? function () {} : _props$onClickClose,
      _props$buttonTitle = props.buttonTitle,
      buttonTitle = _props$buttonTitle === void 0 ? '下一步' : _props$buttonTitle,
      _props$visible = props.visible,
      visible = _props$visible === void 0 ? false : _props$visible,
      _props$innerBorderRad = props.innerBorderRadius,
      innerBorderRadius = _props$innerBorderRad === void 0 ? '16px' : _props$innerBorderRad,
      _props$displayType = props.displayType,
      displayType = _props$displayType === void 0 ? 'inline-block' : _props$displayType,
      _props$handleGoNextSt = props.handleGoNextStep,
      handleGoNextStep = _props$handleGoNextSt === void 0 ? function () {} : _props$handleGoNextSt,
      background = props.background,
      innerStyle = props.innerStyle,
      restProps = _objectWithoutProperties(props, _excluded);

  var popupRef = (0, _react.useRef)({
    open: function open() {},
    close: function close() {}
  });

  var _useState = (0, _react.useState)(props.innerWidth || 0),
      _useState2 = _slicedToArray(_useState, 2),
      innerWidth = _useState2[0],
      setInnerWidth = _useState2[1];

  var _useState3 = (0, _react.useState)(props.innerHeight || 0),
      _useState4 = _slicedToArray(_useState3, 2),
      innerHeight = _useState4[0],
      setInnerHeight = _useState4[1];

  var node = (0, _react.useRef)(null);

  var goNextStep = function goNextStep() {
    popupRef.current.close();
    console.log(popupRef);
    handleGoNextStep && handleGoNextStep();
  };

  var popupAlignMap = {
    bottom: {
      offset: [0, 30],
      overflow: {
        adjustX: true,
        adjustY: true
      },
      targetOffset: [0, 0]
    },
    top: {
      offset: [0, -30],
      overflow: {
        adjustX: true,
        adjustY: true
      },
      targetOffset: [0, 0]
    },
    bottomLeft: {
      offset: [0, 30],
      overflow: {
        adjustX: true,
        adjustY: true
      },
      targetOffset: [0, 0]
    },
    right: {
      offset: [30, 0],
      overflow: {
        adjustX: true,
        adjustY: true
      },
      targetOffset: [0, 0]
    },
    left: {
      offset: [-30, 0],
      overflow: {
        adjustX: true,
        adjustY: true
      },
      targetOffset: [0, 0]
    }
  };
  var placementMap = {
    top: _objectSpread({
      width: innerWidth + 'px',
      height: innerHeight + 'px',
      bottom: "-".concat(innerHeight + 30, "px"),
      left: "50%",
      transform: "translateX(-50%)",
      borderRadius: innerBorderRadius,
      background: background
    }, innerStyle),
    bottom: _objectSpread({
      width: innerWidth + 'px',
      height: innerHeight + 'px',
      top: "-".concat(innerHeight + 28, "px"),
      left: "50%",
      transform: "translateX(-50%)",
      borderRadius: innerBorderRadius,
      background: background
    }, innerStyle),
    left: _objectSpread({
      width: innerWidth + 'px',
      height: innerHeight + 'px',
      right: "-".concat(innerWidth + 30, "px"),
      top: '50%',
      transform: "translateY(-50%)",
      borderRadius: innerBorderRadius,
      background: background
    }, innerStyle),
    right: _objectSpread({
      width: innerWidth + 'px',
      height: innerHeight + 'px',
      top: "50%",
      left: "-".concat(innerWidth + 30, "px"),
      transform: "translateY(-50%)",
      borderRadius: innerBorderRadius,
      background: background
    }, innerStyle),
    bottomLeft: _objectSpread({
      width: innerWidth + 'px',
      height: innerHeight + 'px',
      top: "-".concat(innerHeight + 30, "px"),
      right: 0,
      borderRadius: innerBorderRadius,
      background: background
    }, innerStyle)
  };

  var renderPopupContent = function renderPopupContent() {
    return /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])("".concat(preCls, "__popup")),
      onClick: function onClick(e) {
        e.stopPropagation();
      }
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])(_defineProperty({}, "".concat(preCls, "__mask"), mask))
    }, /*#__PURE__*/_react["default"].createElement("div", {
      style: {
        position: 'absolute',
        cursor: 'not-allowed',
        pointerEvents: 'none'
      }
    })), /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__popup__content"),
      style: {
        width: "".concat(width, "px"),
        height: "".concat(height, "px")
      }
    }, title && /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__popup__content__title")
    }, title), desc && /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__popup__content__desc")
    }, desc), /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__popup__content__bottom")
    }, /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__popup__content__bottom__step")
    }, isShowStep && "".concat(step, "/").concat(total)), /*#__PURE__*/_react["default"].createElement(_button["default"].Rect, {
      text: buttonTitle,
      size: "small",
      onClick: goNextStep,
      style: {
        border: '0px'
      }
    })), /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])("".concat(preCls, "__popup__content-triangle"), "".concat(preCls, "__popup__content-triangle--").concat(popupPlacement))
    }), /*#__PURE__*/_react["default"].createElement("div", {
      className: (0, _classnames["default"])("".concat(preCls, "__popup__content-point"), "".concat(preCls, "__popup__content-point--").concat(popupPlacement))
    })), /*#__PURE__*/_react["default"].createElement("div", {
      className: "".concat(preCls, "__popup__content__border-mask__inner "),
      style: placementMap[popupPlacement]
    }, children));
  };

  (0, _react.useImperativeHandle)(ref, function () {
    return {
      showGuideTip: function showGuideTip() {
        return popupRef.current.open();
      },
      hideGuideTip: function hideGuideTip() {
        return popupRef.current.close();
      }
    };
  });
  (0, _react.useEffect)(function () {
    !innerWidth && !innerHeight && handlerResize();

    function handlerResize() {
      var nodeRect = node.current.getBoundingClientRect();
      var width = nodeRect.width,
          height = nodeRect.height;
      setInnerWidth(width);
      setInnerHeight(height);
    }

    !innerWidth && !innerHeight && window.addEventListener('resize', handlerResize);
    return function () {
      window.removeEventListener('resize', handlerResize);
    };
  }, []);
  return /*#__PURE__*/_react["default"].createElement(_popup["default"], _extends({
    ref: popupRef,
    action: ['focus'],
    mask: false,
    maskClosable: true,
    popupPlacement: popupPlacement,
    popup: renderPopupContent(),
    popupVisible: visible,
    visible: true,
    popupAlign: popupAlignMap[popupPlacement]
  }, restProps), /*#__PURE__*/_react["default"].createElement(_react["default"].Fragment, null, /*#__PURE__*/_react["default"].createElement("div", {
    ref: node,
    className: classnames,
    style: {
      display: displayType
    }
  }, children)));
});
var _default = GuideOrder;
exports["default"] = _default;