"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 (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const router_1 = __importDefault(require("@koa/router"));
const controller = __importStar(require("../controllers/static"));
const budibaseDir_1 = require("../../utilities/budibaseDir");
const authorized_1 = __importDefault(require("../../middleware/authorized"));
const permissions_1 = require("@devlego/backend-core/permissions");
const env = __importStar(require("../../environment"));
const resourceId_1 = require("../../middleware/resourceId");
const router = new router_1.default();
/* istanbul ignore next */
router.param("file", (file, ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
    ctx.file = file && file.includes(".") ? file : "index.html";
    if (!ctx.file.startsWith("budibase-client")) {
        return next();
    }
    // test serves from require
    if (env.isTest()) {
        ctx.devPath = require.resolve("@devlego/client").split(ctx.file)[0];
    }
    else if (env.isDev()) {
        // Serving the client library from your local dir in dev
        ctx.devPath = (0, budibaseDir_1.budibaseTempDir)();
    }
    return next();
}));
// only used in development for retrieving the client library,
// in production the client lib is always stored in the object store.
if (env.isDev()) {
    router.get("/api/assets/client", controller.serveClientLibrary);
}
router
    // TODO: for now this builder endpoint is not authorized/secured, will need to be
    .get("/builder/:file*", controller.serveBuilder)
    .post("/api/attachments/process", (0, authorized_1.default)(permissions_1.BUILDER), controller.uploadFile)
    .post("/api/beta/:feature", controller.toggleBetaUiFeature)
    .post("/api/attachments/:tableId/upload", (0, resourceId_1.paramResource)("tableId"), (0, authorized_1.default)(permissions_1.PermissionTypes.TABLE, permissions_1.PermissionLevels.WRITE), controller.uploadFile)
    .get("/:appId/:path*", controller.serveApp)
    .get("/app/:appUrl/:path*", controller.serveApp)
    .post("/api/attachments/:datasourceId/url", (0, authorized_1.default)(permissions_1.PermissionTypes.TABLE, permissions_1.PermissionLevels.READ), controller.getSignedUploadURL);
exports.default = router;
