/**
 * DevExtreme (renovation/ui/grids/data_grid_next/selection/plugins.js)
 * Version: 22.1.3 (build 22180-0310)
 * Build date: Wed Jun 29 2022
 *
 * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.ToggleSelected = exports.SetSelectedRowKeys = exports.SetSelected = exports.SelectionModeValue = exports.SelectionCheckboxTemplate = exports.SelectedRowKeys = exports.SelectedCount = exports.SelectableItems = exports.SelectableCount = exports.SelectAllModeValue = exports.SelectAllCheckboxTemplate = exports.SelectAll = exports.IsSelected = exports.ClearSelection = exports.AllowSelectAllValue = exports.AddSelectionToRowProperties = exports.AddSelectionToRowClasses = exports.AddSelectionColumnToVisibleColumns = void 0;
var _context = require("../../../../utils/plugin/context");
var _plugins = require("../plugins");
var _utils = require("../utils");
var _row_base = require("../widgets/row_base");
var _classes = _interopRequireDefault(require("../classes"));

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    }
}

function _defineProperty(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        })
    } else {
        obj[key] = value
    }
    return obj
}

function _extends() {
    _extends = Object.assign ? Object.assign.bind() : function(target) {
        for (var i = 1; i < arguments.length; i++) {
            var source = arguments[i];
            for (var key in source) {
                if (Object.prototype.hasOwnProperty.call(source, key)) {
                    target[key] = source[key]
                }
            }
        }
        return target
    };
    return _extends.apply(this, arguments)
}

function _toConsumableArray(arr) {
    return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread()
}

function _nonIterableSpread() {
    throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")
}

function _unsupportedIterableToArray(o, minLen) {
    if (!o) {
        return
    }
    if ("string" === typeof o) {
        return _arrayLikeToArray(o, minLen)
    }
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if ("Object" === n && o.constructor) {
        n = o.constructor.name
    }
    if ("Map" === n || "Set" === n) {
        return Array.from(o)
    }
    if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) {
        return _arrayLikeToArray(o, minLen)
    }
}

function _iterableToArray(iter) {
    if ("undefined" !== typeof Symbol && null != iter[Symbol.iterator] || null != iter["@@iterator"]) {
        return Array.from(iter)
    }
}

function _arrayWithoutHoles(arr) {
    if (Array.isArray(arr)) {
        return _arrayLikeToArray(arr)
    }
}

function _arrayLikeToArray(arr, len) {
    if (null == len || len > arr.length) {
        len = arr.length
    }
    for (var i = 0, arr2 = new Array(len); i < len; i++) {
        arr2[i] = arr[i]
    }
    return arr2
}
var getKey = (0, _utils.createGetKey)("Selection");
var SelectionModeValue = (0, _context.createValue)();
exports.SelectionModeValue = SelectionModeValue;
var AllowSelectAllValue = (0, _context.createValue)();
exports.AllowSelectAllValue = AllowSelectAllValue;
var SelectAllModeValue = (0, _context.createValue)();
exports.SelectAllModeValue = SelectAllModeValue;
var SelectedRowKeys = (0, _context.createValue)();
exports.SelectedRowKeys = SelectedRowKeys;
var SetSelectedRowKeys = (0, _context.createValue)();
exports.SetSelectedRowKeys = SetSelectedRowKeys;
var SelectableItems = (0, _context.createSelector)([SelectAllModeValue, _plugins.LocalData, _plugins.LocalVisibleItems], (function(selectAllModeValue, allItems, visibleItems) {
    return "allPages" === selectAllModeValue ? allItems : visibleItems
}));
exports.SelectableItems = SelectableItems;
var SelectableCount = (0, _context.createSelector)([SelectableItems], (function(selectableItems) {
    return selectableItems.length
}));
exports.SelectableCount = SelectableCount;
var SelectedCount = (0, _context.createSelector)([SelectedRowKeys], (function(selectedRowKeys) {
    return selectedRowKeys.length
}));
exports.SelectedCount = SelectedCount;
var SetSelected = (0, _context.createSelector)([SetSelectedRowKeys, SelectedRowKeys, SelectionModeValue, _plugins.KeyExprPlugin], (function(setSelectedRowKeys, selectedRowKeys, selectionMode, keyExpr) {
    return function(data, value) {
        var key = getKey(data, keyExpr);
        if (value) {
            if ("multiple" === selectionMode) {
                setSelectedRowKeys([].concat(_toConsumableArray(selectedRowKeys), [key]))
            } else {
                setSelectedRowKeys([key])
            }
        } else {
            setSelectedRowKeys(selectedRowKeys.filter((function(i) {
                return i !== key
            })))
        }
    }
}));
exports.SetSelected = SetSelected;
var IsSelected = (0, _context.createSelector)([SelectedRowKeys, _plugins.KeyExprPlugin], (function(selectedRowKeys, keyExpr) {
    return function(data) {
        return selectedRowKeys.includes(getKey(data, keyExpr))
    }
}));
exports.IsSelected = IsSelected;
var ToggleSelected = (0, _context.createSelector)([SetSelected, IsSelected, SelectionModeValue], (function(setSelected, isSelected, selectionMode) {
    return function(data) {
        if ("none" !== selectionMode) {
            var selected = isSelected(data);
            setSelected(data, !selected)
        }
    }
}));
exports.ToggleSelected = ToggleSelected;
var SelectAll = (0, _context.createSelector)([SetSelectedRowKeys, SelectableItems, _plugins.KeyExprPlugin], (function(setSelectedRowKeys, selectableItems, keyExpr) {
    return function() {
        setSelectedRowKeys(selectableItems.map((function(item) {
            return getKey(item, keyExpr)
        })))
    }
}));
exports.SelectAll = SelectAll;
var ClearSelection = (0, _context.createSelector)([SetSelectedRowKeys], (function(setSelectedRowKeys) {
    return function() {
        return setSelectedRowKeys([])
    }
}));
exports.ClearSelection = ClearSelection;
var SelectionCheckboxTemplate = (0, _context.createValue)();
exports.SelectionCheckboxTemplate = SelectionCheckboxTemplate;
var SelectAllCheckboxTemplate = (0, _context.createValue)();
exports.SelectAllCheckboxTemplate = SelectAllCheckboxTemplate;
var AddSelectionColumnToVisibleColumns = (0, _context.createSelector)([_plugins.VisibleColumns, SelectionModeValue, AllowSelectAllValue, SelectionCheckboxTemplate, SelectAllCheckboxTemplate], (function(visibleColumns, selectionMode, allowSelectAll, selectionCheckboxTemplate, selectAllCheckboxTemplate) {
    if ("none" === selectionMode) {
        return visibleColumns
    }
    var selectColumn = {
        cellTemplate: selectionCheckboxTemplate
    };
    if ("multiple" === selectionMode && allowSelectAll) {
        selectColumn.headerTemplate = selectAllCheckboxTemplate
    }
    return [selectColumn].concat(_toConsumableArray(visibleColumns))
}));
exports.AddSelectionColumnToVisibleColumns = AddSelectionColumnToVisibleColumns;
var AddSelectionToRowProperties = (0, _context.createSelector)([_row_base.RowPropertiesGetter, IsSelected], (function(base, isSelected) {
    return function(row) {
        if ("data" === row.rowType && isSelected(row.data)) {
            return _extends({}, base(row), {
                "aria-selected": true
            })
        }
        return base(row)
    }
}));
exports.AddSelectionToRowProperties = AddSelectionToRowProperties;
var AddSelectionToRowClasses = (0, _context.createSelector)([_row_base.RowClassesGetter, IsSelected], (function(base, isSelected) {
    return function(row) {
        if ("data" === row.rowType && isSelected(row.data)) {
            return _extends({}, base(row), _defineProperty({}, _classes.default.selectedRow, true))
        }
        return base(row)
    }
}));
exports.AddSelectionToRowClasses = AddSelectionToRowClasses;
