// Copyright 2025 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.
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());
    });
};
// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.
import * as fs from "fs-extra";
import fetch from "node-fetch";
function snakeCaseify(object) {
    return Object.entries(object).reduce((result, [key, val]) => {
        result[snakeCase(key)] =
            typeof val === "object" && val !== null ? snakeCaseify(val) : val;
        return result;
    }, {});
}
function snakeCase(str) {
    return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
}
/**
 * Create a Settings object with trust configuration.
 * @param trustConfig The trust configuration
 * @returns Settings object that can be passed to Reader/Builder
 */
export function createTrustSettings(trustConfig) {
    return { trust: Object.assign({}, trustConfig) };
}
/**
 * Create a settings object with CAWG trust configuration.
 * @param trustConfig The CAWG trust configuration
 * @returns Settings object that can be passed to Reader/Builder
 */
export function createCawgTrustSettings(trustConfig) {
    return { cawgTrust: Object.assign({}, trustConfig) };
}
/**
 * Create a settings object with verify configuration.
 * @param verifyConfig The verify configuration
 * @returns Settings object that can be passed to Reader/Builder
 */
export function createVerifySettings(verifyConfig) {
    return { verify: Object.assign({}, verifyConfig) };
}
/**
 * Merge multiple settings objects into one.
 * Later settings override earlier ones.
 * @param settings Settings objects to merge
 * @returns Merged settings object
 */
export function mergeSettings(...settings) {
    const merged = {};
    for (const setting of settings) {
        if (setting.trust) {
            merged.trust = Object.assign(Object.assign({}, merged.trust), setting.trust);
        }
        if (setting.cawgTrust) {
            merged.cawgTrust = Object.assign(Object.assign({}, merged.cawgTrust), setting.cawgTrust);
        }
        if (setting.verify) {
            merged.verify = Object.assign(Object.assign({}, merged.verify), setting.verify);
        }
        if (setting.builder) {
            merged.builder = Object.assign(Object.assign({}, merged.builder), setting.builder);
        }
    }
    return merged;
}
/**
 * Convert a settings object to a JSON string.
 * Converts camelCase keys to snake_case to match the c2pa-rs settings format.
 * @param settings The settings object
 * @returns JSON string representation with snake_case keys
 */
export function settingsToJson(settings) {
    return JSON.stringify(snakeCaseify(settings));
}
/**
 * Load settings from a TOML or JSON file.
 * @param filePath The path to the settings file
 * @returns Settings as a string (TOML or JSON depending on file extension)
 */
export function loadSettingsFromFile(filePath) {
    return __awaiter(this, void 0, void 0, function* () {
        const content = yield fs.readFile(filePath, "utf8");
        return content;
    });
}
/**
 * Load settings from a URL.
 * @param url The URL to fetch the settings from
 * @returns Settings as a string
 */
export function loadSettingsFromUrl(url) {
    return __awaiter(this, void 0, void 0, function* () {
        const res = yield fetch(url);
        if (!res.ok) {
            throw new Error(`Failed to fetch settings from URL: ${res.status} ${res.statusText}`);
        }
        return yield res.text();
    });
}
