"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var common_1 = require("../common");
var util_1 = require("./util");
function toResource(args) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var rootPath, filePath, urlPrefix, _a, loadExtensions, file, body, error, type, ext, err_1, message, path, result, _b, folder, file_1, image, markdown, manifestJson, manifestYaml;
        return tslib_1.__generator(this, function (_c) {
            switch (_c.label) {
                case 0:
                    rootPath = args.rootPath, filePath = args.filePath, urlPrefix = args.urlPrefix, _a = args.loadExtensions, loadExtensions = _a === void 0 ? [] : _a;
                    file = common_1.fsPath.parse(filePath);
                    return [4, toFileType(filePath)];
                case 1:
                    type = _c.sent();
                    ext = (file.ext || '').replace(/^\./, '');
                    if (!loadExtensions.includes(ext)) return [3, 5];
                    _c.label = 2;
                case 2:
                    _c.trys.push([2, 4, , 5]);
                    return [4, common_1.fs.readFile(filePath)];
                case 3:
                    body = (_c.sent()).toString();
                    return [3, 5];
                case 4:
                    err_1 = _c.sent();
                    message = "Failed to load body. " + err_1.message;
                    error = { message: message };
                    return [3, 5];
                case 5:
                    path = filePath.substr(rootPath.length);
                    path = urlPrefix ? common_1.fsPath.join(urlPrefix, path) : path;
                    _b = type;
                    switch (_b) {
                        case 'FOLDER': return [3, 6];
                        case 'FILE': return [3, 7];
                        case 'FILE/image': return [3, 8];
                        case 'FILE/markdown': return [3, 10];
                        case 'FILE/json': return [3, 12];
                        case 'FILE/yaml': return [3, 13];
                    }
                    return [3, 14];
                case 6:
                    folder = { type: 'FOLDER', path: path };
                    result = folder;
                    return [3, 15];
                case 7:
                    file_1 = { type: 'FILE', path: path };
                    result = file_1;
                    return [3, 15];
                case 8: return [4, util_1.toManifestImage(filePath, path, { size: true })];
                case 9:
                    image = _c.sent();
                    result = image;
                    return [3, 15];
                case 10: return [4, util_1.toManifestMarkdown(path, body)];
                case 11:
                    markdown = _c.sent();
                    result = markdown;
                    return [3, 15];
                case 12:
                    manifestJson = tslib_1.__assign({ type: 'FILE/json', path: path }, util_1.parseJson(body));
                    result = manifestJson;
                    return [3, 15];
                case 13:
                    manifestYaml = tslib_1.__assign({ type: 'FILE/yaml', path: path }, util_1.parseYaml(body));
                    result = manifestYaml;
                    return [3, 15];
                case 14: throw new Error("File type '" + type + "' not supported.");
                case 15:
                    error = result && result.error ? result.error : error;
                    return [2, result ? common_1.value.deleteUndefined(tslib_1.__assign({}, result, { error: error })) : undefined];
            }
        });
    });
}
exports.toResource = toResource;
function toFileType(path) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var file, ext;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4, common_1.fs.lstat(path)];
                case 1:
                    if ((_a.sent()).isDirectory()) {
                        return [2, 'FOLDER'];
                    }
                    file = common_1.fsPath.parse(path);
                    ext = file.ext.toLowerCase();
                    if (['.md', '.markdown'].includes(ext)) {
                        return [2, 'FILE/markdown'];
                    }
                    if (['.jpg', '.jpeg', '.png', '.svg'].includes(ext)) {
                        return [2, 'FILE/image'];
                    }
                    if (['.json'].includes(ext)) {
                        return [2, 'FILE/json'];
                    }
                    if (['.yml', '.yaml'].includes(ext)) {
                        return [2, 'FILE/yaml'];
                    }
                    return [2, 'FILE'];
            }
        });
    });
}
exports.toFileType = toFileType;
//# sourceMappingURL=util.resource.js.map