import { assert } from "../assert.js";
import { codeToBlob } from "./codeToBlob.js";

/**
 *
 * @param {string} code
 * @param {string} [mime_type]
 * @return {string}
 */
export function codeToURL(code, mime_type = 'application/javascript') {
    assert.isString(code, 'code');
    assert.isString(mime_type, 'mime_type');

    const blob = codeToBlob(code, mime_type);

    if (blob !== undefined && URL.createObjectURL !== undefined) {
        return URL.createObjectURL(blob);
    } else {
        // build data URL in a slowest way possible
        return `data:${mime_type},${encodeURIComponent(code)}`;
    }
}