"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _comp = require("../../ui/src/comp");
var _ui = require("../../ui");
var _dom = require("../../ui/src/dom");
var _vn = require("../../ui/src/vn");
var _xeUtils = _interopRequireDefault(require("xe-utils"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function createReactData() {
  return {
    staticPanes: [],
    cachePaneMaps: {},
    activeKeyFlag: 0,
    openKeyFlag: 0
  };
}
function createInternalData() {
  return {
    // esTime: null,
    activeKeyMaps: {},
    openKeyMaps: {}
  };
}
var _default2 = exports.default = /* define-vxe-component start */(0, _comp.defineVxeComponent)({
  name: 'VxeCollapse',
  mixins: [_ui.globalMixins.sizeMixin, _ui.globalMixins.permissionMixin],
  props: {
    value: Array,
    options: Array,
    padding: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().collapse.padding;
      }
    },
    border: {
      type: Boolean,
      default: function _default() {
        return (0, _ui.getConfig)().collapse.border;
      }
    },
    expandConfig: Object,
    size: {
      type: String,
      default: function _default() {
        return (0, _ui.getConfig)().collapse.size || (0, _ui.getConfig)().size;
      }
    }
  },
  provide: function provide() {
    var $xeCollapse = this;
    return {
      $xeCollapse: $xeCollapse
    };
  },
  data: function data() {
    var xID = _xeUtils.default.uniqueId();
    var reactData = createReactData();
    return Object.assign(Object.assign({}, {}), {
      xID: xID,
      reactData: reactData
    });
  },
  computed: Object.assign(Object.assign({}, {}), {
    computeItemOptions: function computeItemOptions() {
      var $xeCollapse = this;
      var props = $xeCollapse;
      var options = props.options;
      return (options || []).filter(function (item) {
        return $xeCollapse.handleFilterItem(item);
      });
    },
    computeItemStaticOptions: function computeItemStaticOptions() {
      var $xeCollapse = this;
      var reactData = $xeCollapse.reactData;
      var staticPanes = reactData.staticPanes;
      return staticPanes.filter(function (item) {
        return $xeCollapse.handleFilterItem(item);
      });
    },
    computeExpandOpts: function computeExpandOpts() {
      var $xeCollapse = this;
      var props = $xeCollapse;
      return Object.assign({}, (0, _ui.getConfig)().collapse.expandConfig, props.expandConfig);
    },
    computeSpFlag: function computeSpFlag() {
      var $xeCollapse = this;
      var reactData = $xeCollapse.reactData;
      return reactData.staticPanes;
    }
  }),
  methods: {
    //
    // Method
    //
    dispatchEvent: function dispatchEvent(type, params, evnt) {
      var $xeCollapse = this;
      $xeCollapse.$emit(type, (0, _ui.createEvent)(evnt, {
        $collapse: $xeCollapse
      }, params));
    },
    emitModel: function emitModel(value) {
      var $xeCollapse = this;
      $xeCollapse.$emit('input', value);
    },
    handleFilterItem: function handleFilterItem(item) {
      var permissionCode = item.permissionCode;
      if (permissionCode) {
        if (!_ui.permission.checkVisible(permissionCode)) {
          return false;
        }
      }
      return true;
    },
    updateModel: function updateModel() {
      var $xeCollapse = this;
      var props = $xeCollapse;
      var internalData = $xeCollapse.internalData;
      var reactData = $xeCollapse.reactData;
      var modelValue = props.value;
      var openKeyMaps = internalData.openKeyMaps;
      var activeMaps = {};
      if (modelValue) {
        modelValue.forEach(function (name) {
          if (!openKeyMaps[name]) {
            openKeyMaps[name] = true;
          }
          activeMaps[name] = true;
        });
      }
      internalData.activeKeyMaps = activeMaps;
      reactData.activeKeyFlag++;
      reactData.openKeyFlag++;
    },
    handleOpen: function handleOpen(name) {
      var $xeCollapse = this;
      var internalData = $xeCollapse.internalData;
      var openKeyMaps = internalData.openKeyMaps;
      if (name && !openKeyMaps[name]) {
        openKeyMaps[name] = true;
        $xeCollapse.dispatchEvent('load', {
          name: name
        }, null);
        return true;
      }
      return false;
    },
    initDefaultName: function initDefaultName(list) {
      var $xeCollapse = this;
      var reactData = $xeCollapse.reactData;
      var internalData = $xeCollapse.internalData;
      var activeKeyMaps = internalData.activeKeyMaps;
      var nameMaps = {};
      if (list && list.length) {
        list.forEach(function (item) {
          var _ref = item || {},
            name = _ref.name,
            preload = _ref.preload;
          if (name) {
            var isActive = activeKeyMaps[name];
            nameMaps[name] = {
              height: 0,
              loading: false
            };
            if (isActive || preload) {
              $xeCollapse.handleOpen(name);
            }
          }
        });
      }
      reactData.cachePaneMaps = nameMaps;
      reactData.openKeyFlag++;
    },
    callSlot: function callSlot(slotFunc, params, h) {
      var $xeCollapse = this;
      var slots = $xeCollapse.$scopedSlots;
      if (slotFunc) {
        if (_xeUtils.default.isString(slotFunc)) {
          slotFunc = slots[slotFunc] || null;
        }
        if (_xeUtils.default.isFunction(slotFunc)) {
          return (0, _vn.getSlotVNs)(slotFunc.call($xeCollapse, params, h));
        }
      }
      return [];
    },
    handleClickEvent: function handleClickEvent(evnt, item) {
      var $xeCollapse = this;
      var internalData = $xeCollapse.internalData;
      var reactData = $xeCollapse.reactData;
      var activeKeyMaps = internalData.activeKeyMaps;
      var esTime = internalData.esTime;
      var expandOpts = $xeCollapse.computeExpandOpts;
      var accordion = expandOpts.accordion;
      var name = item.name;
      if (esTime) {
        clearTimeout(esTime);
      }
      if (name) {
        var headEl = evnt.currentTarget;
        var bodyEl = headEl.nextElementSibling;
        var expanded = false;
        if (activeKeyMaps[name]) {
          delete activeKeyMaps[name];
        } else {
          if (accordion) {
            activeKeyMaps = {};
          }
          expanded = true;
          activeKeyMaps[name] = true;
        }
        var startAnimation = function startAnimation() {
          var itemEl = bodyEl.firstElementChild;
          if (itemEl) {
            bodyEl.style.height = (0, _dom.toCssUnit)(itemEl.scrollHeight);
            if (expanded) {
              internalData.esTime = setTimeout(function () {
                bodyEl.style.height = '';
                internalData.esTime = undefined;
              }, 200);
            } else {
              $xeCollapse.$nextTick(function () {
                requestAnimationFrame(function () {
                  bodyEl.style.height = '';
                });
              });
            }
          }
        };
        var value = Object.keys(activeKeyMaps);
        internalData.activeKeyMaps = activeKeyMaps;
        $xeCollapse.handleOpen(name);
        reactData.activeKeyFlag++;
        reactData.openKeyFlag++;
        if (bodyEl) {
          var itemEl = bodyEl.firstElementChild;
          if (itemEl) {
            startAnimation();
          } else {
            bodyEl.style.height = '0';
            $xeCollapse.$nextTick(startAnimation);
          }
        }
        $xeCollapse.emitModel(value);
        $xeCollapse.dispatchEvent('change', {
          value: value,
          name: name
        }, evnt);
        $xeCollapse.dispatchEvent('toggle-expand', {
          value: value,
          name: name,
          expanded: expanded
        }, evnt);
      }
    },
    //
    // Render
    //
    renderList: function renderList(h, itemList) {
      var $xeCollapse = this;
      var reactData = $xeCollapse.reactData;
      var internalData = $xeCollapse.internalData;
      var activeKeyFlag = reactData.activeKeyFlag,
        openKeyFlag = reactData.openKeyFlag;
      var activeKeyMaps = internalData.activeKeyMaps,
        openKeyMaps = internalData.openKeyMaps;
      var expandOpts = $xeCollapse.computeExpandOpts;
      return itemList.map(function (item) {
        var icon = item.icon,
          name = item.name,
          title = item.title,
          slots = item.slots;
        var titleSlot = slots ? slots.title : null;
        var defaultSlot = slots ? slots.default : null;
        var isActive = name && activeKeyFlag && activeKeyMaps[name];
        return h('div', {
          class: 'vxe-collapse-item'
        }, [h('div', {
          class: 'vxe-collapse--item-header',
          on: {
            click: function click(evnt) {
              $xeCollapse.handleClickEvent(evnt, item);
            }
          }
        }, [expandOpts.showIcon ? h('span', {
          class: 'vxe-collapse--item-switch'
        }, [h('i', {
          class: isActive ? (0, _ui.getIcon)().COLLAPSE_OPEN : (0, _ui.getIcon)().COLLAPSE_CLOSE
        })]) : (0, _ui.renderEmptyElement)($xeCollapse), icon ? h('span', {
          class: 'vxe-collapse--item-icon'
        }, [h('i', {
          class: icon
        })]) : (0, _ui.renderEmptyElement)($xeCollapse), h('span', {
          class: 'vxe-collapse--item-name'
        }, titleSlot ? $xeCollapse.callSlot(titleSlot, {
          name: name,
          title: title
        }, h) : "".concat(title))]), h('div', {
          class: ['vxe-collapse--item-content', {
            'is--visible': isActive,
            'is--hidden': !isActive
          }]
        }, [name && openKeyFlag && openKeyMaps[name] ? h('div', {
          class: 'vxe-collapse--item-inner'
        }, [defaultSlot ? $xeCollapse.callSlot(defaultSlot, {
          name: name,
          title: title
        }, h) : '']) : (0, _ui.renderEmptyElement)($xeCollapse)])]);
      });
    },
    renderVN: function renderVN(h) {
      var $xeCollapse = this;
      var props = $xeCollapse;
      var slots = $xeCollapse.$scopedSlots;
      var padding = props.padding,
        border = props.border;
      var vSize = $xeCollapse.computeSize;
      var itemOptions = $xeCollapse.computeItemOptions;
      var itemStaticOptions = $xeCollapse.computeItemStaticOptions;
      var defaultSlot = slots.default;
      var itemList = defaultSlot ? itemStaticOptions : itemOptions;
      return h('div', {
        ref: 'refElem',
        class: ['vxe-collapse', _defineProperty(_defineProperty(_defineProperty({}, "size--".concat(vSize), vSize), 'is--padding', padding), 'is--border', border)]
      }, [h('div', {
        class: 'vxe-collapse-slots'
      }, defaultSlot ? defaultSlot({}) : []), $xeCollapse.renderList(h, itemList)]);
    }
  },
  watch: {
    value: function value() {
      var $xeCollapse = this;
      $xeCollapse.updateModel();
    },
    options: function options() {
      var $xeCollapse = this;
      var props = $xeCollapse;
      $xeCollapse.initDefaultName(props.options);
    },
    computeSpFlag: function computeSpFlag() {
      var $xeCollapse = this;
      var reactData = $xeCollapse.reactData;
      $xeCollapse.initDefaultName(reactData.staticPanes);
    }
  },
  created: function created() {
    var $xeCollapse = this;
    var props = $xeCollapse;
    var reactData = $xeCollapse.reactData;
    $xeCollapse.internalData = createInternalData();
    $xeCollapse.updateModel();
    $xeCollapse.initDefaultName(reactData.staticPanes.length ? reactData.staticPanes : props.options);
  },
  destroyed: function destroyed() {
    var $xeCollapse = this;
    var reactData = $xeCollapse.reactData;
    var internalData = $xeCollapse.internalData;
    var esTime = internalData.esTime;
    if (esTime) {
      clearTimeout(esTime);
    }
    _xeUtils.default.assign(reactData, createReactData());
    _xeUtils.default.assign(internalData, createInternalData());
  },
  render: function render(h) {
    return this.renderVN(h);
  }
});
/* define-vxe-component end */