"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const types_1 = require("../types");
const promises_1 = require("node:fs/promises");
const Local_1 = require("../Local");
const URI_1 = require("../utils/URI");
const uniqid_1 = __importDefault(require("../utils/uniqid"));
const Queue_1 = require("../queues/Queue");
const Remove_1 = __importDefault(require("../queues/Remove"));
const trash = import("trash");
const App_1 = __importDefault(require("../App"));
const ramda_1 = require("ramda");
async function default_1(files, force, connPath, immediately = false) {
    if (!files.length) {
        return;
    }
    if (!force) {
        App_1.default.onError({ type: types_1.FailureType.ConfirmDeletion, files });
        return;
    }
    if ((0, URI_1.isLocal)(files[0])) {
        const paths = files.map(p => (0, URI_1.parseURI)(p)['path']);
        if (immediately) {
            paths.forEach(path => (0, promises_1.rm)(path, { recursive: true, force: true }));
        }
        else {
            (await trash).default(paths);
        }
        paths.forEach(path => {
            if (path.startsWith(connPath) && !(0, Local_1.stat)(path)?.dir) {
                // TODO delete from credentials
            }
        });
    }
    else {
        const connId = (0, URI_1.parseURI)(files[0])['id'];
        const id = (0, uniqid_1.default)();
        const queue = new Remove_1.default(connId, files.map((0, ramda_1.pipe)(URI_1.parseURI, (0, ramda_1.prop)('path'))), state => App_1.default.onQueueUpdate(id, { type: types_1.QueueEventType.Update, state }), error => {
            App_1.default.onError({
                type: types_1.FailureType.RemoteError,
                id: this.connId,
                message: error.message ?? String(error)
            });
        }, () => {
            Queue_1.queues.delete(id);
            App_1.default.onQueueUpdate(id, { type: types_1.QueueEventType.Complete });
        }, App_1.default.remoteWatcher);
        Queue_1.queues.set(id, queue);
        App_1.default.onQueueUpdate(id, { type: types_1.QueueEventType.Create, queueType: types_1.QueueType.Remove, connection: connId });
        queue.create();
    }
}
