"use strict";
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 });
exports.fetchFormsList = fetchFormsList;
exports.fetchFormEntries = fetchFormEntries;
exports.fetchBusinessPlanEntries = fetchBusinessPlanEntries;
// src/services/wpGravityForms.ts
const axios_1 = __importDefault(require("axios"));
const WP_BASE_URL = process.env.WP_BASE_URL || "";
const GF_API_KEY = process.env.GF_API_KEY || "";
const GF_API_SECRET = process.env.GF_API_SECRET || "";
// Construit une URL complète à partir de WP_BASE_URL
function buildWpUrl(path) {
    return WP_BASE_URL.replace(/\/$/, "") + path;
}
/**
 * Récupère la liste des formulaires Gravity Forms
 * GET /wp-json/gf/v2/forms
 */
function fetchFormsList() {
    return __awaiter(this, void 0, void 0, function* () {
        var _a, _b, _c;
        const url = buildWpUrl("/wp-json/gf/v2/forms");
        try {
            // Première version : SANS auth car votre curl fonctionne déjà sans authentification.
            const res = yield axios_1.default.get(url, {
                timeout: 15000,
            });
            return res.data; // objet { "6": {...}, "7": {...}, ... }
        }
        catch (error) {
            console.error("❌ Erreur Gravity Forms LISTE:", {
                status: (_a = error.response) === null || _a === void 0 ? void 0 : _a.status,
                data: (_b = error.response) === null || _b === void 0 ? void 0 : _b.data,
                url,
            });
            throw new Error(`API Gravity Forms (forms list) : ${((_c = error.response) === null || _c === void 0 ? void 0 : _c.status) || ""}`);
        }
    });
}
/**
 * Récupère les entrées d’un formulaire donné
 * GET /wp-json/gf/v2/forms/{formId}/entries
 */
function fetchFormEntries(formId) {
    return __awaiter(this, void 0, void 0, function* () {
        var _a, _b, _c;
        const url = buildWpUrl(`/wp-json/gf/v2/forms/${formId}/entries`);
        try {
            // Ici, vous pouvez garder l’auth si nécessaire.
            const res = yield axios_1.default.get(url, {
                auth: {
                    username: GF_API_KEY,
                    password: GF_API_SECRET,
                },
                params: {
                    page_size: 100,
                },
                timeout: 20000,
            });
            return res.data; // { total_count, entries: [...] }
        }
        catch (error) {
            console.error("❌ Erreur Gravity Forms ENTRIES:", {
                status: (_a = error.response) === null || _a === void 0 ? void 0 : _a.status,
                data: (_b = error.response) === null || _b === void 0 ? void 0 : _b.data,
                url,
            });
            throw new Error(`API Gravity Forms (entries form ${formId}) : ${((_c = error.response) === null || _c === void 0 ? void 0 : _c.status) || ""}`);
        }
    });
}
/**
 * Récupère les entrées du formulaire Business Plan (id = 7)
 */
function fetchBusinessPlanEntries() {
    return __awaiter(this, void 0, void 0, function* () {
        const BUSINESS_PLAN_FORM_ID = 7;
        return fetchFormEntries(BUSINESS_PLAN_FORM_ID);
    });
}
