/**
 * tdesign v0.41.7
 * (c) 2022 tdesign
 * @license MIT
 */

'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var _asyncToGenerator = require('@babel/runtime/helpers/asyncToGenerator');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _regeneratorRuntime = require('@babel/runtime/regenerator');
var Vue = require('vue');
var utils_classnames = require('../utils/classnames.js');
var anchor_utils = require('./utils.js');
var utils_dom = require('../utils/dom.js');
var anchor_props = require('./props.js');
var utils_renderTnode = require('../utils/render-tnode.js');
var affix_index = require('../affix/index.js');
var anchor_constant = require('./constant.js');
require('../config.js');
require('raf');
require('lodash/isString');
require('../utils/easing.js');
require('@babel/runtime/helpers/typeof');
require('../affix/affix.js');
require('lodash/isFunction');
require('../affix/props.js');
require('../utils/withInstall.js');
require('lodash/capitalize');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var _asyncToGenerator__default = /*#__PURE__*/_interopDefaultLegacy(_asyncToGenerator);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var _regeneratorRuntime__default = /*#__PURE__*/_interopDefaultLegacy(_regeneratorRuntime);
var Vue__default = /*#__PURE__*/_interopDefaultLegacy(Vue);

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__default["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 ANCHOR_LINE_CLASSNAME = "".concat(anchor_constant.COMPONENT_NAME, "__line");
var ANCHOR_LINE_CURSOR_CLASSNAME = "".concat(anchor_constant.COMPONENT_NAME, "__line-cursor");
var _Anchor = Vue__default["default"].extend({
  name: "TAnchor",
  props: _objectSpread({}, anchor_props["default"]),
  provide: function provide() {
    return {
      tAnchor: this
    };
  },
  data: function data() {
    return {
      links: [],
      active: "",
      activeLineStyle: false
    };
  },
  watch: {
    attach: function attach() {
      if (this.scrollContainer) {
        utils_dom.off(this.scrollContainer, "scroll", this.handleScroll);
      }

      this.getScrollContainer();
    }
  },
  methods: {
    getScrollContainer: function getScrollContainer() {
      var container = this.container;
      this.scrollContainer = utils_dom.getScrollContainer(container);
      utils_dom.on(this.scrollContainer, "scroll", this.handleScroll);
      this.handleScroll();
    },
    getAnchorTarget: function getAnchorTarget(link) {
      var matcher = link.match(anchor_utils.ANCHOR_SHARP_REGEXP);

      if (!matcher) {
        return;
      }

      var anchor = document.getElementById(matcher[1]);

      if (!anchor) {
        return;
      }

      return anchor;
    },
    registerLink: function registerLink(link) {
      var links = this.links;

      if (!anchor_utils.ANCHOR_SHARP_REGEXP.test(link) || links.indexOf(link) !== -1) {
        return;
      }

      links.push(link);
    },
    unregisterLink: function unregisterLink(link) {
      this.links = this.links.filter(function (each) {
        return each !== link;
      });
    },
    setCurrentActiveLink: function setCurrentActiveLink(link) {
      var _this = this;

      return _asyncToGenerator__default["default"]( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee() {
        var active;
        return _regeneratorRuntime__default["default"].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                active = _this.active;

                if (!(active === link)) {
                  _context.next = 3;
                  break;
                }

                return _context.abrupt("return");

              case 3:
                _this.active = link;

                _this.emitChange(link, active);

                _context.next = 7;
                return Vue__default["default"].nextTick();

              case 7:
                _this.updateActiveLine();

              case 8:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }))();
    },
    updateActiveLine: function updateActiveLine() {
      var ele = this.$el.querySelector(".".concat(utils_classnames["default"].STATUS.active, ">a"));

      if (!ele) {
        this.activeLineStyle = false;
        return;
      }

      var top = ele.offsetTop,
          height = ele.offsetHeight;
      this.activeLineStyle = {
        top: "".concat(top, "px"),
        height: "".concat(height, "px"),
        opacity: 1
      };
    },
    emitChange: function emitChange(currentLink, prevLink) {
      this.$emit("change", currentLink, prevLink);

      if (this.onChange) {
        this.onChange(currentLink, prevLink);
      }
    },
    handleLinkClick: function handleLinkClick(link) {
      this.$emit("click", link);

      if (this.onClick) {
        this.onClick(link);
      }
    },
    handleScrollTo: function handleScrollTo(link) {
      var _this2 = this;

      return _asyncToGenerator__default["default"]( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee2() {
        var anchor, scrollContainer, targetOffset, scrollTop, offsetTop, top;
        return _regeneratorRuntime__default["default"].wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                anchor = _this2.getAnchorTarget(link);

                _this2.setCurrentActiveLink(link);

                if (anchor) {
                  _context2.next = 4;
                  break;
                }

                return _context2.abrupt("return");

              case 4:
                _this2.handleScrollLock = true;
                scrollContainer = _this2.scrollContainer, targetOffset = _this2.targetOffset;
                scrollTop = utils_dom.getScroll(scrollContainer);
                offsetTop = anchor_utils.getOffsetTop(anchor, scrollContainer);
                top = scrollTop + offsetTop - targetOffset;
                _context2.next = 11;
                return utils_dom.scrollTo(top, {
                  container: scrollContainer
                });

              case 11:
                _this2.handleScrollLock = false;

              case 12:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }))();
    },
    handleScroll: function handleScroll() {
      var _this3 = this;

      if (this.handleScrollLock) return;
      var links = this.links,
          bounds = this.bounds,
          targetOffset = this.targetOffset;
      var filters = [];
      var active = "";
      links.forEach(function (link) {
        var anchor = _this3.getAnchorTarget(link);

        if (!anchor) {
          return;
        }

        var top = anchor_utils.getOffsetTop(anchor, _this3.scrollContainer);

        if (top < bounds + targetOffset) {
          filters.push({
            link: link,
            top: top
          });
        }
      });

      if (filters.length) {
        var latest = filters.reduce(function (prev, cur) {
          return prev.top > cur.top ? prev : cur;
        });
        active = latest.link;
      }

      this.setCurrentActiveLink(active);
    },
    renderCursor: function renderCursor() {
      var h = this.$createElement;
      var titleContent = utils_renderTnode.renderTNodeJSX(this, "cursor");
      return titleContent || h("div", {
        "class": ANCHOR_LINE_CURSOR_CLASSNAME
      });
    }
  },
  mounted: function mounted() {
    var _this4 = this;

    return _asyncToGenerator__default["default"]( /*#__PURE__*/_regeneratorRuntime__default["default"].mark(function _callee3() {
      var active;
      return _regeneratorRuntime__default["default"].wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              active = _this4.active;

              _this4.getScrollContainer();

              if (!active) {
                _context3.next = 6;
                break;
              }

              _context3.next = 5;
              return Vue__default["default"].nextTick();

            case 5:
              _this4.handleScrollTo(active);

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    }))();
  },
  destroyed: function destroyed() {
    if (!this.scrollContainer) return;
    utils_dom.off(this.scrollContainer, "scroll", this.handleScroll);
  },
  render: function render() {
    var h = arguments[0];
    var children = this.$scopedSlots["default"],
        size = this.size,
        affixProps = this.affixProps,
        activeLineStyle = this.activeLineStyle;
    var className = [anchor_constant.COMPONENT_NAME, utils_classnames["default"].SIZE[size]];
    var content = h("div", {
      "class": className
    }, [h("div", {
      "class": ANCHOR_LINE_CLASSNAME
    }, [h("div", {
      "class": "".concat(ANCHOR_LINE_CURSOR_CLASSNAME, "-wrapper"),
      "style": activeLineStyle
    }, [this.renderCursor()])]), children && children(null)]);

    if (affixProps) {
      return h(affix_index.Affix, {
        "props": _objectSpread({}, affixProps)
      }, [content]);
    }

    return content;
  }
});

exports["default"] = _Anchor;
//# sourceMappingURL=anchor.js.map
