/**
 * DevExtreme (cjs/ui/file_manager/ui.file_manager.dialog.folder_chooser.js)
 * Version: 23.2.6 (build 24140-0112)
 * Build date: Sun May 19 2024
 *
 * Copyright (c) 2012 - 2024 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
"use strict";
exports.default = void 0;
var _renderer = _interopRequireDefault(require("../../core/renderer"));
var _extend = require("../../core/utils/extend");
var _message = _interopRequireDefault(require("../../localization/message"));
var _uiFile_manager = require("./ui.file_manager.common");
var _uiFile_manager2 = _interopRequireDefault(require("./ui.file_manager.dialog"));
var _uiFile_manager3 = _interopRequireDefault(require("./ui.file_manager.files_tree_view"));

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

function _inheritsLoose(subClass, superClass) {
    subClass.prototype = Object.create(superClass.prototype);
    subClass.prototype.constructor = subClass;
    _setPrototypeOf(subClass, superClass)
}

function _setPrototypeOf(o, p) {
    _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function(o, p) {
        o.__proto__ = p;
        return o
    };
    return _setPrototypeOf(o, p)
}
const FILE_MANAGER_DIALOG_FOLDER_CHOOSER = "dx-filemanager-dialog-folder-chooser";
const FILE_MANAGER_DIALOG_FOLDER_CHOOSER_POPUP = "dx-filemanager-dialog-folder-chooser-popup";
let FileManagerFolderChooserDialog = function(_FileManagerDialogBas) {
    _inheritsLoose(FileManagerFolderChooserDialog, _FileManagerDialogBas);

    function FileManagerFolderChooserDialog() {
        return _FileManagerDialogBas.apply(this, arguments) || this
    }
    var _proto = FileManagerFolderChooserDialog.prototype;
    _proto.show = function() {
        var _this$_filesTreeView;
        this._setSelectedDirInfo(null);
        null === (_this$_filesTreeView = this._filesTreeView) || void 0 === _this$_filesTreeView ? void 0 : _this$_filesTreeView.refresh();
        _FileManagerDialogBas.prototype.show.call(this)
    };
    _proto.switchToCopyDialog = function(targetItemInfos) {
        this._targetItemInfos = targetItemInfos;
        this._setTitle(_message.default.format("dxFileManager-dialogDirectoryChooserCopyTitle"));
        this._setApplyButtonOptions({
            text: _message.default.format("dxFileManager-dialogDirectoryChooserCopyButtonText"),
            disabled: true
        })
    };
    _proto.switchToMoveDialog = function(targetItemInfos) {
        this._targetItemInfos = targetItemInfos;
        this._setTitle(_message.default.format("dxFileManager-dialogDirectoryChooserMoveTitle"));
        this._setApplyButtonOptions({
            text: _message.default.format("dxFileManager-dialogDirectoryChooserMoveButtonText"),
            disabled: true
        })
    };
    _proto._getDialogOptions = function() {
        return (0, _extend.extend)(_FileManagerDialogBas.prototype._getDialogOptions.call(this), {
            contentCssClass: FILE_MANAGER_DIALOG_FOLDER_CHOOSER,
            popupCssClass: FILE_MANAGER_DIALOG_FOLDER_CHOOSER_POPUP
        })
    };
    _proto._createContentTemplate = function(element) {
        _FileManagerDialogBas.prototype._createContentTemplate.call(this, element);
        this._filesTreeView = this._createComponent((0, _renderer.default)("<div>"), _uiFile_manager3.default, {
            getDirectories: this.option("getDirectories"),
            getCurrentDirectory: () => this._getDialogSelectedDirectory(),
            onDirectoryClick: e => this._onFilesTreeViewDirectoryClick(e),
            onFilesTreeViewContentReady: () => this._toggleUnavailableLocationsDisabled(true)
        });
        this._$contentElement.append(this._filesTreeView.$element())
    };
    _proto._getDialogResult = function() {
        const result = this._getDialogSelectedDirectory();
        return result ? {
            folder: result
        } : result
    };
    _proto._getDefaultOptions = function() {
        return (0, _extend.extend)(_FileManagerDialogBas.prototype._getDefaultOptions.call(this), {
            getItems: null
        })
    };
    _proto._getDialogSelectedDirectory = function() {
        return this._selectedDirectoryInfo
    };
    _proto._onFilesTreeViewDirectoryClick = function(_ref) {
        let {
            itemData: itemData
        } = _ref;
        this._setSelectedDirInfo(itemData);
        this._filesTreeView.updateCurrentDirectory()
    };
    _proto._setSelectedDirInfo = function(dirInfo) {
        this._selectedDirectoryInfo = dirInfo;
        this._setApplyButtonOptions({
            disabled: !dirInfo
        })
    };
    _proto._onPopupShown = function() {
        this._toggleUnavailableLocationsDisabled(true);
        _FileManagerDialogBas.prototype._onPopupShown.call(this)
    };
    _proto._onPopupHiding = function() {
        this._toggleUnavailableLocationsDisabled(false);
        _FileManagerDialogBas.prototype._onPopupHiding.call(this)
    };
    _proto._toggleUnavailableLocationsDisabled = function(isDisabled) {
        if (!this._filesTreeView) {
            return
        }
        const locations = this._getLocationsToProcess(isDisabled);
        this._filesTreeView.toggleDirectoryExpandedStateRecursive(locations.locationsToExpand[0], isDisabled).then(() => this._filesTreeView.toggleDirectoryLineExpandedState(locations.locationsToCollapse, !isDisabled).then(() => locations.locationKeysToDisable.forEach(key => this._filesTreeView.toggleNodeDisabledState(key, isDisabled))))
    };
    _proto._getLocationsToProcess = function(isDisabled) {
        const expandLocations = {};
        const collapseLocations = {};
        this._targetItemInfos.forEach(itemInfo => {
            if (itemInfo.parentDirectory) {
                expandLocations[itemInfo.parentDirectory.getInternalKey()] = itemInfo.parentDirectory
            }
            if (itemInfo.fileItem.isDirectory) {
                collapseLocations[itemInfo.getInternalKey()] = itemInfo
            }
        });
        const expandMap = (0, _uiFile_manager.getMapFromObject)(expandLocations);
        const collapseMap = (0, _uiFile_manager.getMapFromObject)(collapseLocations);
        return {
            locationsToExpand: isDisabled ? expandMap.values : [],
            locationsToCollapse: isDisabled ? collapseMap.values : [],
            locationKeysToDisable: expandMap.keys.concat(...collapseMap.keys)
        }
    };
    return FileManagerFolderChooserDialog
}(_uiFile_manager2.default);
var _default = FileManagerFolderChooserDialog;
exports.default = _default;
module.exports = exports.default;
module.exports.default = exports.default;
