export function hasChild(context, scope) {
    var _a = context.props, childNumKey = _a.childNumKey, childKey = _a.childKey, row = scope.row;
    if (row[childNumKey] != undefined) {
        return row[childNumKey] > 0 ? true : false;
    }
    if (row[childKey] != undefined) {
        return row[childKey].length > 0 ? true : false;
    }
    else {
        return false;
    }
}
function hasChildInData(context, scope) {
    var _a = context.props, childNumKey = _a.childNumKey, childKey = _a.childKey, treeKey = _a.treeKey, parentKey = _a.parentKey, data = scope.store.states._data, row = scope.row;
    return data.filter(function (d) { return d[parentKey] == row[treeKey]; }).length > 0;
}
export function paddingLeft(context, scope) {
    return (parseInt(scope.row[context.props.levelKey]) * parseInt(context.props.indentSize.toString())) + 'px';
}
function removeCachedExpanedRow(context, scope) {
    var _treeCachedExpanded = scope.store.states._treeCachedExpanded, treeKey = context.props.treeKey, row = scope.row;
    scope.store.states._treeCachedExpanded = _treeCachedExpanded.filter(function (crow) { return crow[treeKey] != row[treeKey]; });
}
function isCachedExpanedRow(context, scope) {
    var _treeCachedExpanded = scope.store.states._treeCachedExpanded, treeKey = context.props.treeKey, row = scope.row;
    return _treeCachedExpanded.map(function (row) { return row[treeKey]; }).filter(function (_treeKey) { return _treeKey == row[treeKey]; }).length > 0;
}
var isUnExpanded = function (context, scope) {
    var row = scope.row, data = scope.store.states._data, _treeRowExpanded = scope.store.states._treeRowExpanded, key = context.props.treeKey, parentKey = context.props.parentKey;
    var _hasChild = hasChild(context, scope);
    if (!_hasChild)
        return false;
    var IsRowShowed = data.some(function (item) { return item[key] == row[key]; });
    if (!IsRowShowed)
        return false;
    var isInexpanded = scope.store.states._treeRowExpanded.some(function (treeKey) { return treeKey[context.props.treeKey] == scope.row[context.props.treeKey]; });
    if (!isInexpanded)
        return false;
    return !hasChildInData(context, scope);
};
export function isNeedExpanedRow(context, scope) {
    if (context.props.expandAll &&
        !scope.store.states._treeInitedExpanded.some(function (treeKey) { return treeKey == scope.row[context.props.treeKey]; })) {
        scope.store.states._treeInitedExpanded.push(scope.row[context.props.treeKey]);
        return true;
    }
    if (isLoadingRow(context, scope))
        return false;
    if (isUnExpanded(context, scope)) {
        scope.store.states._treeRowExpanded = scope.store.states._treeRowExpanded
            .filter(function (ex) { return ex[context.props.treeKey] != scope.row[context.props.treeKey]; });
        return true;
    }
    var expandKey = context.props.expandKey, row = scope.row;
    if (expandKey && row[expandKey]
        &&
            !scope.store.states._treeInitedExpanded.some(function (treeKey) { return treeKey == row[context.props.treeKey]; })) {
        scope.store.states._treeInitedExpanded.push(scope.row[context.props.treeKey]);
        return true;
    }
    var result = isCachedExpanedRow(context, scope);
    if (result)
        removeCachedExpanedRow(context, scope);
    return result;
}
export function isLoadingRow(context, scope) {
    var _treeRowLoading = scope.store.states._treeRowLoading, treeKey = context.props.treeKey, row = scope.row;
    return _treeRowLoading.map(function (row) { return row[treeKey]; }).filter(function (_treeKey) { return _treeKey == row[treeKey]; }).length > 0;
}
export function isExpandedRow(context, scope) {
    var _treeRowExpanded = scope.store.states._treeRowExpanded, treeKey = context.props.treeKey, row = scope.row;
    return _treeRowExpanded.map(function (row) { return row[treeKey]; }).filter(function (_treeKey) { return _treeKey == row[treeKey]; }).length > 0;
}
export function icon(scope, context) {
    if (isLoadingRow(context, scope))
        return 'el-icon-loading';
    if (isExpandedRow(context, scope))
        return 'el-icon-caret-bottom';
    return 'el-icon-caret-right';
}
export function folderIcon(context, scope) {
    var floder = context.props.folderIcon, floder_open = context.props.folderIcon + '-open';
    return isExpandedRow(context, scope) ? floder_open : floder;
}
export function renderDetail(h, context, scope) {
    if (context.data.scopedSlots && context.data.scopedSlots.default) {
        return context.data.scopedSlots.default(scope);
    }
    if (context.props.formatter) {
        return <span>{context.props.formatter(scope.row, scope.column)}</span>;
    }
    return <span>{"  "}{scope.row[context.props.prop]}</span>;
}
