var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
    if (kind === "m") throw new TypeError("Private method is not writable");
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
    return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
    if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
    if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
    return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};
var _UIClient_renderContext;
import { EffectType, Form, ToastAppearance } from '@devvit/protos';
import { assertValidFormFields } from '../../../../apis/ui/helpers/assertValidFormFields.js';
import { transformFormFields } from '../../../../apis/ui/helpers/transformForm.js';
import { _activeRenderContext } from './BlocksHandler.js';
import { getFormDefinition } from './useForm.js';
export function useUI() {
    const renderContext = _activeRenderContext;
    if (!renderContext) {
        throw new Error('useUI can only be called from within the top level of a component.');
    }
    return new UIClient(renderContext);
}
export class UIClient {
    constructor(renderContext) {
        _UIClient_renderContext.set(this, void 0);
        __classPrivateFieldSet(this, _UIClient_renderContext, renderContext, "f");
    }
    showForm(formKey, data) {
        const formDefinition = getFormDefinition(__classPrivateFieldGet(this, _UIClient_renderContext, "f"), formKey);
        if (!formDefinition) {
            throw new Error('Form does not exist. Make sure you have added it using useForm.');
        }
        const formData = formDefinition.form instanceof Function
            ? formDefinition.form(data ?? {})
            : formDefinition.form;
        const form = {
            fields: [],
            id: formKey,
            title: formData.title,
            acceptLabel: formData.acceptLabel,
            cancelLabel: formData.cancelLabel,
            shortDescription: formData.description,
        };
        assertValidFormFields(formData.fields);
        form.fields = transformFormFields(formData.fields);
        __classPrivateFieldGet(this, _UIClient_renderContext, "f").emitEffect(formKey, {
            type: EffectType.EFFECT_SHOW_FORM,
            showForm: {
                form,
            },
        });
    }
    showToast(textOrToast) {
        let toast;
        if (textOrToast instanceof Object) {
            toast = {
                text: textOrToast.text,
                appearance: textOrToast.appearance === 'success' ? ToastAppearance.SUCCESS : ToastAppearance.NEUTRAL,
            };
        }
        else {
            toast = {
                text: textOrToast,
            };
        }
        __classPrivateFieldGet(this, _UIClient_renderContext, "f").emitEffect(textOrToast.toString(), {
            type: EffectType.EFFECT_SHOW_TOAST,
            showToast: {
                toast,
            },
        });
    }
    navigateTo(thingOrUrl) {
        let url;
        if (typeof thingOrUrl === 'string') {
            // Validate URL
            url = new URL(thingOrUrl).toString();
        }
        else {
            url = new URL(thingOrUrl.permalink, 'https://www.reddit.com').toString();
        }
        __classPrivateFieldGet(this, _UIClient_renderContext, "f").emitEffect(url, {
            type: EffectType.EFFECT_NAVIGATE_TO_URL,
            navigateToUrl: {
                url,
            },
        });
    }
}
_UIClient_renderContext = new WeakMap();
