"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.stringify = stringify;
const clone_1 = require("../util/clone");
const escape_1 = require("./escape");
const globalUrl_1 = require("./globalUrl");
const stringifySpec_1 = require("./stringifySpec");
function stringify(value, deref = false) {
    const t = typeof value;
    switch (t) {
        case 'string':
            return `"${(0, escape_1.escape)(value)}"`;
        case 'number':
        case 'boolean':
            return `${value}`;
        case 'object':
            if (value === null) {
                return 'null';
            }
            else if (Array.isArray(value)) {
                return `[${value.map((v) => stringify(v, deref)).join(',')}]`;
            }
            else {
                if (!value.constructor || value.constructor.name === 'Object') {
                    return `{${Object.entries(value)
                        .filter(([key, value]) => {
                        return value !== undefined;
                    })
                        .map(([key, value]) => `${`"${(0, escape_1.escape)(key)}"`}:${stringify(value)}`)
                        .join(',')}}`;
                }
                else {
                    if (deref) {
                        if (value.__?.includes('U')) {
                            return stringify(value.constructor);
                        }
                        else {
                            return 'null';
                        }
                    }
                    else {
                        return (0, globalUrl_1.globalUrl)(value.__global_id);
                    }
                }
            }
            break;
        case 'function':
            if (value.__bundle) {
                const { __bundle } = value;
                const bundle = (0, clone_1.clone)(__bundle);
                (0, stringifySpec_1.stringifyUnitBundleSpecData)(bundle);
                return `$${stringify(bundle, deref)}`;
            }
            else {
                // throw new Error('invalid unit class')
                return 'null';
            }
        case 'undefined':
            return '';
        default:
            throw new Error('cannot stringify value');
    }
}
//# sourceMappingURL=stringify.js.map