import "./index.css";
import _Array$isArray from "@babel/runtime-corejs2/core-js/array/is-array";
import _JSON$stringify from "@babel/runtime-corejs2/core-js/json/stringify";

/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
import { createComponentName, componentMixin } from '../_utils/component';
import request from '../request';
import Menu from '../Menu';
import SubMenu from '../Menu/SubMenu';
import MenuItem from '../Menu/Item';
import Badge from '../Badge';
import defaultData from './default-data';
import { toRelativePath, includeUrl } from '../_utils/url';
const cacheKey = 'gaoding-navigation'; // FIXME: 目前写的有点混乱

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

  data() {
    return {
      navs: [...defaultData]
    };
  },

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

  },

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

  mounted() {
    request.get('/api/navigations').then(res => {
      this.navs = res.data.filter(d => d.title);

      try {
        localStorage.setItem(cacheKey, _JSON$stringify(res.data));
      } catch (error) {
        console.error(error);
      }
    });
  },

  methods: {
    indexPath() {
      if (!this.$isServer) {
        const 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 (const nav of navs) {
        if (this.includeHref(nav.link)) {
          value.push(this.transformUrl(nav.link));
          return value;
        } else if (nav.children) {
          const v = this.getIndexPath(nav.children, [...value, this.transformUrl(nav.link)]);
          if (v.length) return v;
        }
      }

      return [];
    },

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

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

    onSelect(index, indexPath) {
      const data = indexPath.reduce((navs, link, index) => {
        if (!_Array$isArray(navs)) return navs;
        const nav = navs.find(nav => 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 () {
  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 ($event) {
          _vm.$emit("sub-menu:show", item.id, item);
        },
        hide: function ($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 */

const __vue_inject_styles__ = undefined;
/* scoped */

const __vue_scope_id__ = undefined;
/* module identifier */

const __vue_module_identifier__ = undefined;
/* functional template */

const __vue_is_functional_template__ = false;
/* component normalizer */

import __vue_normalize__ from '../_utils/vue-normalize-component.js';
/* style inject */

/* style inject SSR */

export default __vue_normalize__({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);