"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _classnames = _interopRequireDefault(require("classnames"));
var _Dock = _interopRequireDefault(require("../../../styles/cspace-ui/Dock.css"));
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 (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; }
/* global window */

const propTypes = {
  children: _propTypes.default.element,
  dockTop: _propTypes.default.number,
  isSidebarOpen: _propTypes.default.bool
};
const defaultProps = {
  isSidebarOpen: true
};
class Dock extends _react.Component {
  constructor(props) {
    super(props);
    this.setDomNode = this.setDomNode.bind(this);
    this.handleScroll = this.handleScroll.bind(this);
    this.state = {
      docked: false
    };
  }
  componentDidMount() {
    window.addEventListener('scroll', this.handleScroll, false);
  }
  componentWillUnmount() {
    window.removeEventListener('scroll', this.handleScroll, false);
  }
  handleScroll() {
    const node = this.domNode;
    if (!node) return;
    const {
      dockTop
    } = this.props;
    const {
      docked
    } = this.state;
    if (docked) {
      if (window.scrollY + dockTop < node.offsetTop) {
        this.setState({
          docked: false
        });
      }
    } else if (window.scrollY + dockTop >= node.offsetTop) {
      this.setState({
        docked: true
      });
    }
  }
  setDomNode(ref) {
    this.domNode = ref;
  }
  render() {
    const {
      children,
      dockTop,
      isSidebarOpen
    } = this.props;
    const {
      docked
    } = this.state;
    const className = (0, _classnames.default)(isSidebarOpen ? _Dock.default.normal : _Dock.default.full, {
      [_Dock.default.docked]: docked
    });
    const inlineStyle = docked ? {
      height: this.domNode.offsetHeight
    } : {};
    const innerInlineStyle = docked ? {
      top: dockTop || 0
    } : {};
    return /*#__PURE__*/_react.default.createElement("header", {
      className: className,
      style: inlineStyle,
      ref: this.setDomNode
    }, /*#__PURE__*/_react.default.createElement("div", {
      style: innerInlineStyle
    }, children));
  }
}
exports.default = Dock;
Dock.propTypes = propTypes;
Dock.defaultProps = defaultProps;