"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

require("core-js/modules/es6.regexp.search");

var _getIterator2 = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/get-iterator"));

var _isArray2 = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/array/is-array"));

require("./index.css");

var _stringify = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/json/stringify"));

var _component = require("../_utils/component");

var _request = _interopRequireDefault(require("../request"));

var _Menu = _interopRequireDefault(require("../Menu"));

var _SubMenu = _interopRequireDefault(require("../Menu/SubMenu"));

var _Item = _interopRequireDefault(require("../Menu/Item"));

var _Badge = _interopRequireDefault(require("../Badge"));

var _defaultData = _interopRequireDefault(require("./default-data"));

var _url = require("../_utils/url");

var _vueNormalizeComponent = _interopRequireDefault(require("../_utils/vue-normalize-component.js"));

/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
var cacheKey = 'gaoding-navigation'; // FIXME: 目前写的有点混乱

var __vue_script__ = {
  name: (0, _component.createComponentName)('navigation'),
  components: {
    Menu: _Menu.default,
    SubMenu: _SubMenu.default,
    MenuItem: _Item.default,
    Badge: _Badge.default
  },
  mixins: [_component.componentMixin],
  props: {
    // 当前页面的域名, 会将相同域名的地址转换为相对路径
    host: {
      type: String
    },
    // 是否隐藏二级菜单
    isHideMenu: {
      type: Function
    },
    // 修改激活状态的路由
    transformValue: {
      type: Function
    }
  },

  data() {
    return {
      navs: [].concat(_defaultData.default)
    };
  },

  watch: {
    $route() {
      this.$forceUpdate();
    }

  },

  beforeMount() {
    try {
      if (localStorage.getItem(cacheKey)) {
        // 简单处理, 暂时不考虑缓存时间
        var data = JSON.parse(localStorage.getItem(cacheKey));
        this.navs = data;
      }
    } catch (error) {
      console.error(error);
    }
  },

  mounted() {
    var _this = this;

    _request.default.get('/api/navigations').then(function (res) {
      _this.navs = res.data.filter(function (d) {
        return d.title;
      });

      try {
        localStorage.setItem(cacheKey, (0, _stringify.default)(res.data));
      } catch (error) {
        console.error(error);
      }
    });
  },

  methods: {
    indexPath() {
      if (!this.$isServer) {
        var value = this.getIndexPath(this.navs, []);
        if (this.transformValue) return this.transformValue(value, this);
        return value;
      }

      return [];
    },

    _isHideMenu(nav) {
      if (!this.isHideMenu) return false;
      return this.isHideMenu(this.transformUrl(nav.link), this.indexPath(), nav);
    },

    getIndexPath(navs, value) {
      for (var _iterator = navs, _isArray = (0, _isArray2.default)(_iterator), _i = 0, _iterator = _isArray ? _iterator : (0, _getIterator2.default)(_iterator);;) {
        var _ref;

        if (_isArray) {
          if (_i >= _iterator.length) break;
          _ref = _iterator[_i++];
        } else {
          _i = _iterator.next();
          if (_i.done) break;
          _ref = _i.value;
        }

        var nav = _ref;

        if (this.includeHref(nav.link)) {
          value.push(this.transformUrl(nav.link));
          return value;
        } else if (nav.children) {
          var v = this.getIndexPath(nav.children, [].concat(value, [this.transformUrl(nav.link)]));
          if (v.length) return v;
        }
      }

      return [];
    },

    transformUrl(url) {
      return (0, _url.toRelativePath)(url, this.host);
    },

    includeHref(url) {
      var href = this.host ? location.protocol + '//' + this.host + location.pathname + location.search : location.href;
      return (0, _url.includeUrl)(href, url);
    },

    onSelect(index, indexPath) {
      var data = indexPath.reduce(function (navs, link, index) {
        if (!(0, _isArray2.default)(navs)) return navs;
        var nav = navs.find(function (nav) {
          return nav.link === link;
        });
        if (nav) return index === indexPath.length - 1 ? nav : nav.children;
        return nav;
      }, this.navs);
      /**
      * 当选择时触发
      * @arg index: {string} menu-item索引
      * @arg indexPath: {string[]} 完整路径
      */

      this.$emit('select', index, indexPath, data);
    }

  }
};
/* template */

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c("Menu", {
    class: _vm.bem(),
    attrs: {
      indexPath: _vm.indexPath(),
      router: ""
    },
    on: {
      select: _vm.onSelect
    }
  }, [_vm._l(_vm.navs, function (item) {
    return [!item.children || item.children.length === 0 ? _c("menu-item", {
      key: item.id,
      attrs: {
        index: _vm.transformUrl(item.link),
        route: _vm.transformUrl(item.link)
      }
    }, [_c("span", [_vm._v(_vm._s(item.title))])]) : _c("sub-menu", {
      key: item.id,
      attrs: {
        index: _vm.transformUrl(item.link),
        route: _vm.transformUrl(item.link),
        placement: "bottom-start",
        offset: "-36px, 0, 0, 0",
        "hide-menu": _vm._isHideMenu(item),
        title: item.title,
        "popover-class": _vm.bem("popover")
      },
      on: {
        show: function show($event) {
          _vm.$emit("sub-menu:show", item.id, item);
        },
        hide: function hide($event) {
          _vm.$emit("sub-menu:hide", item.id, item);
        }
      }
    }, _vm._l(item.children, function (menu) {
      return _c("menu-item", {
        key: menu.id,
        attrs: {
          index: _vm.transformUrl(item.link),
          route: _vm.transformUrl(menu.link)
        }
      }, [menu.is_new ? _c("badge", {
        attrs: {
          value: "New"
        }
      }, [_vm._v(_vm._s(menu.title))]) : _c("span", [_vm._v(_vm._s(menu.title))])], 1);
    }), 1)];
  })], 2);
};

var __vue_staticRenderFns__ = [];
__vue_render__._withStripped = true;
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* component normalizer */

/* style inject */

/* style inject SSR */
var _default = (0, _vueNormalizeComponent.default)({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

exports.default = _default;