/**
 * ldesign v0.2.2
 * (c) 2022年9月20日 swimly
 * @license MIT
 */

import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
import _defineProperty from '@babel/runtime/helpers/defineProperty';
import _regeneratorRuntime from '@babel/runtime/regenerator';
import Vue from 'vue';
import { ANCHOR_SHARP_REGEXP, getOffsetTop } from './utils.js';
import { off, getScrollContainer, on, getScroll, scrollTo } from '../../utils/dom.js';
import { AnchorProps } from './props.js';
import { renderLNodeJSX } from '../../utils/render-node.js';
import { getClassPrefixMixins } from '../config-provider/config-receiver.js';
import mixins from '../../utils/mixins.js';
import { Affix } from '../affix/index.js';
import 'raf';
import 'lodash/isString';
import '../../utils/easing.js';
import '@babel/runtime/helpers/typeof';
import 'lodash/camelCase';
import 'lodash/kebabCase';
import 'lodash/mergeWith';
import 'lodash';
import '../config-provider/context.js';
import '../../chunks/zh_CN.js';
import '../config-provider/type.js';
import '../affix/affix.js';
import 'lodash/isFunction';
import '../affix/props.js';
import '../../utils/with-install.js';
import '../../config.js';
import '../affix/style';
import '../affix/type.js';

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; }
var classPrefixMixins = getClassPrefixMixins("anchor");
var _Anchor = mixins(Vue, classPrefixMixins).extend({
  name: "LAnchor",
  components: {
    LAffix: Affix
  },
  props: _objectSpread({}, AnchorProps),
  provide: function provide() {
    return {
      tAnchor: this
    };
  },
  data: function data() {
    return {
      links: [],
      active: "",
      activeLineStyle: false
    };
  },
  watch: {
    attach: function attach() {
      if (this.scrollContainer) {
        off(this.scrollContainer, "scroll", this.handleScroll);
      }

      this.getScrollContainer();
    },
    size: {
      handler: function handler() {
        var _this = this;

        return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
          return _regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  _context.next = 2;
                  return Vue.nextTick();

                case 2:
                  _this.updateActiveLine();

                case 3:
                case "end":
                  return _context.stop();
              }
            }
          }, _callee);
        }))();
      },
      immediate: true
    }
  },
  methods: {
    getScrollContainer: function getScrollContainer$1() {
      var container = this.container;
      this.scrollContainer = getScrollContainer(container);
      on(this.scrollContainer, "scroll", this.handleScroll);
      this.handleScroll();
    },
    getAnchorTarget: function getAnchorTarget(link) {
      var matcher = link.match(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_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 _this2 = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
        var active;
        return _regeneratorRuntime.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                active = _this2.active;

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

                return _context2.abrupt("return");

              case 3:
                _this2.active = link;

                _this2.emitChange(link, active);

                _context2.next = 7;
                return Vue.nextTick();

              case 7:
                _this2.updateActiveLine();

              case 8:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2);
      }))();
    },
    updateActiveLine: function updateActiveLine() {
      var ele = this.$el.querySelector(".".concat(this.commonStatusClassName.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 _this3 = this;

      return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
        var anchor, scrollContainer, targetOffset, scrollTop, offsetTop, top;
        return _regeneratorRuntime.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                anchor = _this3.getAnchorTarget(link);

                _this3.setCurrentActiveLink(link);

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

                return _context3.abrupt("return");

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

              case 11:
                _this3.handleScrollLock = false;

              case 12:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3);
      }))();
    },
    handleScroll: function handleScroll() {
      var _this4 = 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 = _this4.getAnchorTarget(link);

        if (!anchor) {
          return;
        }

        var top = getOffsetTop(anchor, _this4.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 = renderLNodeJSX(this, "cursor");
      return titleContent || h("div", {
        "class": "".concat(this.componentName, "__line-cursor")
      });
    }
  },
  mounted: function mounted() {
    var _this5 = this;

    return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
      var active;
      return _regeneratorRuntime.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              active = _this5.active;

              _this5.getScrollContainer();

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

              _context4.next = 5;
              return Vue.nextTick();

            case 5:
              _this5.handleScrollTo(active);

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    }))();
  },
  destroyed: function destroyed() {
    if (!this.scrollContainer) return;
    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 = [this.componentName, this.commonSizeClassName[size]];
    var content = h("div", {
      "class": className
    }, [h("div", {
      "class": "".concat(this.componentName, "__line")
    }, [h("div", {
      "class": "".concat(this.componentName, "__line-cursor-wrapper"),
      "style": activeLineStyle
    }, [this.renderCursor()])]), children && children(null)]);

    if (affixProps) {
      return h("l-affix", {
        "props": _objectSpread({}, affixProps)
      }, [content]);
    }

    return content;
  }
});

export { _Anchor as default };
//# sourceMappingURL=anchor.js.map
