"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteAdminProductType = exports.updateAdminProductType = exports.createAdminProductType = exports.getAdminProductTypeById = exports.listAdminProductTypes = void 0;
const productTypeService_js_1 = require("../services/productTypeService.js");
const apiResponse_js_1 = require("../utils/apiResponse.js");
const getStaffId = (req) => {
    if (req.auth?.actorType === "staff") {
        return req.auth.staff.id;
    }
    throw new Error("UNAUTHORIZED");
};
const listAdminProductTypes = async (req, res) => {
    const query = req.validatedQuery;
    const result = await (0, productTypeService_js_1.listProductTypes)(query);
    return (0, apiResponse_js_1.sendSuccess)(res, 200, result);
};
exports.listAdminProductTypes = listAdminProductTypes;
const getAdminProductTypeById = async (req, res) => {
    const { id } = req.validatedParams;
    const productType = await (0, productTypeService_js_1.getProductTypeById)(id);
    if (!productType) {
        return (0, apiResponse_js_1.sendError)(res, 404, "PRODUCT_TYPE_NOT_FOUND", "Product type not found.");
    }
    return (0, apiResponse_js_1.sendSuccess)(res, 200, productType);
};
exports.getAdminProductTypeById = getAdminProductTypeById;
const createAdminProductType = async (req, res) => {
    const input = req.validatedBody;
    const staffId = getStaffId(req);
    try {
        const productType = await (0, productTypeService_js_1.createProductType)(input, staffId);
        return (0, apiResponse_js_1.sendSuccess)(res, 201, productType, "Product type created.");
    }
    catch (error) {
        if (error.message === "PRODUCT_TYPE_NOT_FOUND") {
            return (0, apiResponse_js_1.sendError)(res, 404, "PRODUCT_TYPE_NOT_FOUND", "Product type not found.");
        }
        throw error;
    }
};
exports.createAdminProductType = createAdminProductType;
const updateAdminProductType = async (req, res) => {
    const { id } = req.validatedParams;
    const input = req.validatedBody;
    const staffId = getStaffId(req);
    try {
        const productType = await (0, productTypeService_js_1.updateProductType)(id, input, staffId);
        return (0, apiResponse_js_1.sendSuccess)(res, 200, productType, "Product type updated.");
    }
    catch (error) {
        if (error.message === "PRODUCT_TYPE_NOT_FOUND") {
            return (0, apiResponse_js_1.sendError)(res, 404, "PRODUCT_TYPE_NOT_FOUND", "Product type not found.");
        }
        throw error;
    }
};
exports.updateAdminProductType = updateAdminProductType;
const deleteAdminProductType = async (req, res) => {
    const { id } = req.validatedParams;
    try {
        await (0, productTypeService_js_1.deleteProductType)(id);
        return (0, apiResponse_js_1.sendSuccess)(res, 200, null, "Product type deleted.");
    }
    catch (error) {
        if (error.message === "PRODUCT_TYPE_NOT_FOUND") {
            return (0, apiResponse_js_1.sendError)(res, 404, "PRODUCT_TYPE_NOT_FOUND", "Product type not found.");
        }
        throw error;
    }
};
exports.deleteAdminProductType = deleteAdminProductType;
//# sourceMappingURL=productTypeController.js.map