"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.fpath = void 0;
const path = __importStar(require("path-browserify"));
/**
 * fpath utility mirrors node [path](https://nodejs.org/docs/latest/api/path.html)
 * module, but accepts IFilePath type instead of strings. It is useful when
 * working with [sos.fileSystem](/sdk/sos/fileSystem).
 *
 * Not implemented functions:
 * - format
 * - matchesGlob
 * - parse
 * - relative
 *
 * @position 99
 *
 * @example
 * import { sos, fpath } from "@signageos/front-applet";
 *
 * const [internal] = await sos.fileSystem.listInternalStorageUnits();
 * const rootPath = {
 * 	filePath: '', // Empty string is used as an absolute path instead of "/"
 * 	storageUnit: internal
 * };
 *
 * // list saved files in videos/2025-05-19/ directory
 * const videos = await sos.fileSystem.listFiles(
 *  fpath.join(rootPath, "videos", "2025-05-19"),
 * );
 */
exports.fpath = {
    /**
     * Return the last portion of path, since it is not a valid path, a string is returned.
     *
     * @example
     * const path = { filePath: "images/picture.png", storageUnit: ... };
     * fpath.basename(path); // "picture.png"
     */
    basename(filePath, suffix) {
        return path.basename(filePath.filePath, suffix);
    },
    /**
     * Removes the last portion of path, returning the parent directory of the path. Ignores trailing slashes
     *
     * @example
     * const path = { filePath: "images/picture.png", storageUnit: ... };
     * fpath.dirname(path); // { filePath: "images", storageUnit: ... }
     */
    dirname(filePath) {
        let parentPath = path.dirname(filePath.filePath);
        if (parentPath === '.') {
            // The top level file dirname is ".", but sos accepts empty string "" for root
            parentPath = '';
        }
        return {
            filePath: parentPath,
            storageUnit: filePath.storageUnit,
        };
    },
    /**
     * Returns extension of the path, from the last period, including the period.
     *
     * @example
     * const path = { filePath: "images/picture.png", storageUnit: ... };
     * fpath.dirname(path); // .png
     */
    extname(filePath) {
        return path.extname(filePath.filePath);
    },
    /**
     * Always returns true, because all file paths are absolute
     */
    isAbsolute(_) {
        return true;
    },
    /**
     * Returns new filePath with paths appended to it and normalized (resolved . and ..)
     *
     * @example
     * const path = { filePath: "images", storageUnit: ... };
     * fpath.join(path, "racoons", ".", "picture.png"); // { filePath: "images/racoons/picture.png", storageUnit: ... }
     */
    join(filePath, ...paths) {
        return {
            filePath: path.join(filePath.filePath, ...paths),
            storageUnit: filePath.storageUnit,
        };
    },
    /**
     * Similar to fpath.join, but resulting path will always be subdirectory of base
     *
     * @example
     * const path = { filePath: "uploads/userA", storageUnit: ... };
     * fpath.safeJoin(path, "..", "userB", "picture.png"); // { filePath: "uploads/userA/userB/picture.png", storageUnit: ... }
     */
    safeJoin(base, ...paths) {
        return {
            filePath: base.filePath + path.resolve('/', ...paths),
            storageUnit: base.storageUnit,
        };
    },
    /**
     * Resolves . and .. in the path and removes multiple slashes
     *
     * @example
     * const path = { filePath: "images//test/../test2/./", storageUnit: ... };
     * fpath.normalize(path); // { filePath: "images/test2/", storageUnit: ... }
     */
    normalize(filePath) {
        return {
            filePath: path.normalize(filePath.filePath),
            storageUnit: filePath.storageUnit,
        };
    },
    /**
     * Same as fpath.join()
     */
    resolve(filePath, ...paths) {
        return exports.fpath.join(filePath, ...paths);
    },
    /**
     * Separator used for joining path segments
     */
    sep: path.sep,
    /**
     * Concatenate fp with paths without adding separator
     *
     * @example
     * const path = { filePath: "uploads/archive.tar", storageUnit: ... };
     * fpath.concat(path, "_extracted"); // { filePath: "uploads/archive.tar_extracted", storageUnit: ... }
     */
    concat(filePath, ...paths) {
        return {
            filePath: filePath.filePath.concat(...paths),
            storageUnit: filePath.storageUnit,
        };
    },
    /**
     * Convert filePath to string, this string is not guaranteed to be unique and should be only used for debugging/logging
     */
    stringify(filePath) {
        return path.join(`[${filePath.storageUnit.type}]`, filePath.filePath);
    },
    /**
     * Underlying path polyfill
     */
    path,
};
//# sourceMappingURL=fpath.js.map