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

/*
 * @Author: codingfly
 * @Description:
 * @Date: 2021-08-04 14:29:13
 * @LastEditTime: 2021-09-16 17:03:30
 * @FilePath: \my-remax-ui\components\notice-bar\index.tsx
 */
import classNames from 'classnames';
import React, { useEffect, useRef, useState } from 'react';
import { View, Text } from 'remax/one';
import { createSelectorQuery } from '../one';
import { getPrefixCls } from '../common';
import { guid } from '../_utils/utils';
import Icon from '../icon';
import Space from '../space';
import rpxToPx from '../rpx-to-px';
var prefixCls = getPrefixCls('notice-bar');

var NoticeBar = function NoticeBar(props) {
  var _props$icon = props.icon,
      icon = _props$icon === void 0 ? "notification" : _props$icon,
      _props$color = props.color,
      color = _props$color === void 0 ? "#ff9d46" : _props$color,
      _props$backgroundColo = props.backgroundColor,
      backgroundColor = _props$backgroundColo === void 0 ? "#fefcec" : _props$backgroundColo,
      style = props.style,
      data = props.data,
      className = props.className;
  var inerId = useRef(guid());
  var intervalId = useRef(null);

  var _useState = useState(0),
      _useState2 = _slicedToArray(_useState, 2),
      distance = _useState2[0],
      setDistance = _useState2[1];

  var _useState3 = useState(0),
      _useState4 = _slicedToArray(_useState3, 2),
      contentW = _useState4[0],
      setContentW = _useState4[1];

  useEffect(function () {
    var getContentWidth = function getContentWidth() {
      createSelectorQuery() // @ts-ignore
      .select(".".concat(prefixCls, "-content")).boundingClientRect().exec(function (rect) {
        var node = Array.isArray(rect) ? rect === null || rect === void 0 ? void 0 : rect[0] : rect;
        setContentW(node === null || node === void 0 ? void 0 : node.width);
      });
    };

    getContentWidth();
    return function () {
      clearInterval(intervalId.current);
    };
  }, []);
  useEffect(function () {
    var getSwiperWidth = function getSwiperWidth() {
      createSelectorQuery() // @ts-ignore
      .select(".".concat(prefixCls, "-id-").concat(inerId.current)).selectAll('text').boundingClientRect().exec(function (rect) {
        var node = Array.isArray(rect) ? rect === null || rect === void 0 ? void 0 : rect[0] : rect;
        setDistance(node === null || node === void 0 ? void 0 : node.width);
      });
    };

    getSwiperWidth();
    return function () {
      clearInterval(intervalId.current);
    };
  }, []);
  var classes = classNames("".concat(prefixCls, "-noticeInner"), _defineProperty({}, "".concat(prefixCls, "-id-").concat(inerId.current), true));
  return /*#__PURE__*/React.createElement(View, {
    className: classNames(prefixCls, className),
    style: Object.assign(Object.assign({}, style), {
      color: color,
      backgroundColor: backgroundColor
    })
  }, /*#__PURE__*/React.createElement(View, {
    className: "".concat(prefixCls, "-icon")
  }, /*#__PURE__*/React.createElement(Icon, {
    type: icon,
    size: 20,
    color: color
  })), /*#__PURE__*/React.createElement(View, {
    className: "".concat(prefixCls, "-title")
  }, "\u6E29\u99A8\u63D0\u793A:"), /*#__PURE__*/React.createElement(View, {
    className: "".concat(prefixCls, "-content")
  }, /*#__PURE__*/React.createElement(View, {
    className: classes,
    style: {
      animationDuration: "".concat((rpxToPx(distance) || 0) / 20, "s"),
      paddingLeft: contentW + 'Px'
    }
  }, /*#__PURE__*/React.createElement(Space, {
    size: "middle"
  }, data.map(function (item, index) {
    return /*#__PURE__*/React.createElement(Text, {
      key: index
    }, item);
  })))));
};

export default NoticeBar;