"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Immutable = require("immutable");
const core_1 = require("./core");
const primitives_1 = require("./primitives");
const html_1 = require("./html");
const combinators_1 = require("./combinators");
exports.simple_inner_form = function (mode, model_name, entries) {
    return c => combinators_1.repeat(`${model_name(c.model)}_repeater`)(c => combinators_1.any(`${model_name(c.model)}_inner_form`)(entries.map(e => e.kind == "string" ?
        combinators_1.retract(`${model_name(c.model)}_${e.field_name}_retract`)(c => e.in(c.model), c => s => {
            let new_c = e.out(c.model)(s);
            let errors = e.get_errors(new_c);
            return { model: new_c, errors: errors.length > 0 ? c.errors.set(e.field_name, errors) : c.errors.remove(e.field_name) };
        }, html_1.label(e.field_name, true)(html_1.div(`monadic-field ${c.errors.has(e.field_name) ? "monadic-field-error" : ""}`)(primitives_1.string(mode, "text", `${model_name(c.model)}_${e.field_name}`))))
        : e.kind == "number" ?
            combinators_1.retract(`${model_name(c.model)}_${e.field_name}_retract`)(c => e.in(c.model), c => s => {
                let new_c = e.out(c.model)(s);
                let errors = e.get_errors(new_c);
                return { model: new_c, errors: errors.length > 0 ? c.errors.set(e.field_name, errors) : c.errors.remove(e.field_name) };
            }, html_1.label(e.field_name, true)(html_1.div(`monadic-field ${c.errors.has(e.field_name) ? "monadic-field-error" : ""}`)(primitives_1.number(mode, `${model_name(c.model)}_${e.field_name}`))))
            : e.kind == "image" ?
                combinators_1.retract(`${model_name(c.model)}_${e.field_name}_retract`)(c => e.in(c.model), c => s => {
                    let new_c = e.out(c.model)(s);
                    let errors = e.get_errors(new_c);
                    return { model: new_c, errors: errors.length > 0 ? c.errors.set(e.field_name, errors) : c.errors.remove(e.field_name) };
                }, html_1.label(e.field_name, true)(html_1.div(`monadic-field ${c.errors.has(e.field_name) ? "monadic-field-error" : ""}`)(html_1.image(mode, `${model_name(c.model)}_${e.field_name}`))))
                : e.kind == "lazy image" ?
                    combinators_1.retract(`${model_name(c.model)}_${e.field_name}_retract`)(c => null, c => _ => c, _ => e.download(c.model).then(`${model_name(c.model)}_${e.field_name}_downloader`, src => combinators_1.repeat()((src) => html_1.label(e.field_name, true)(html_1.image(mode, `${model_name(c.model)}_${e.field_name}`))(src).then(`${model_name(c.model)}_${e.field_name}_uploader`, new_src => e.upload(c.model)(new_src)))(src)).ignore())
                    : e.kind == "file" ?
                        combinators_1.retract(`${model_name(c.model)}_${e.field_name}_retract`)(c => e.in(c.model), c => s => {
                            let new_c = e.out(c.model)(s);
                            let errors = e.get_errors(new_c);
                            return { model: new_c, errors: errors.length > 0 ? c.errors.set(e.field_name, errors) : c.errors.remove(e.field_name) };
                        }, html_1.label(e.field_name, true)(html_1.div(`monadic-field ${c.errors.has(e.field_name) ? "monadic-field-error" : ""}`)(_ => html_1.file(mode, e.filename(c.model), e.url(c.model)).ignore_with(null))))
                        : e.kind == "lazy file" ?
                            combinators_1.retract(`${model_name(c.model)}_${e.field_name}_retract`)(c => null, c => f => (Object.assign({}, c, { model: e.out(c.model)(f) })), _ => html_1.label(e.field_name, true)(_ => html_1.file(mode, e.filename(c.model), e.url(c.model)).then(`${model_name(c.model)}_${e.field_name}_uploader`, f => e.upload(c.model)(f).ignore_with(f)))(null))
                            : e.kind == "datetime" ?
                                combinators_1.retract(`${model_name(c.model)}_${e.field_name}_retract`)(c => e.in(c.model), c => s => {
                                    let new_c = e.out(c.model)(s);
                                    let errors = e.get_errors(new_c);
                                    return { model: new_c, errors: errors.length > 0 ? c.errors.set(e.field_name, errors) : c.errors.remove(e.field_name) };
                                }, primitives_1.date_time(mode, e.field_name, () => "Creating date_time formfield"))
                                :
                                    null))(c))(c);
};
exports.form_errors = function (model_name, entries) {
    return fd => combinators_1.any(`form-errors`)(entries.map(e => e.kind != "lazy image" && e.kind != "image" ?
        c => c.errors.has(e.field_name) ?
            primitives_1.string("view", "text", `${model_name(c.model)}_${e.field_name}`)(`${c.errors.get(e.field_name)}`).ignore(`${model_name(c.model)}_${e.field_name}_error_ignore`).never()
            :
                core_1.unit(null).never()
        :
            c => core_1.unit(null).never()))(fd).filter(_ => false);
};
exports.simple_form_with_autosave = function (mode, model_name, entries, download_M, upload_M) {
    return download_M.then(undefined, c => exports.simple_inner_form(mode, model_name, entries)({ model: c, errors: Immutable.Map() })
        .then(`${model_name(c)}_error_recap`, combinators_1.any()([
        c => exports.form_errors(model_name, entries)(c).ignore_with(c).filter(_ => false),
        c => core_1.unit(c)
    ]))
        .filter(c => c.errors.isEmpty(), `${model_name(c)}_error_filter`)
        .map(c => c.model).then(`${model_name(c)}_uploader`, combinators_1.delay(200, `${model_name(c)}_delay`)(upload_M)).ignore());
};
exports.simple_form_with_save_button = function (mode, model_name, entries, download_M, upload_M) {
    return download_M.then(undefined, c => exports.simple_inner_form(mode, model_name, entries)({ model: c, errors: Immutable.Map() }).then(`${model_name(c)}_form`, c => combinators_1.any()([
        exports.form_errors(model_name, entries),
        c => html_1.button(`save`, !c.errors.isEmpty())(c)
    ])(c)).map(c => c.model).then(`${model_name(c)}_uploader`, combinators_1.delay(200, `${model_name(c)}_delayer`)(upload_M)).ignore());
};
exports.simple_form_with_prev_and_next_buttons = function (mode, model_name, entries, prev_enabled, next_enabled, prev_visible, next_visible, on_prev, on_next) {
    return c => exports.simple_inner_form(mode, model_name, entries)(c).then(`${model_name(c.model)}_form`, c => combinators_1.any()([
        exports.form_errors(model_name, entries),
        c => prev_visible(c) ? html_1.button(`prev`, prev_enabled(c))(c).map(c => (Object.assign({}, c, { model: on_prev(c.model) }))) : core_1.unit(c).filter(_ => false),
        c => next_visible(c) ? html_1.button(`next`, next_enabled(c))(c).map(c => (Object.assign({}, c, { model: on_next(c.model) }))) : core_1.unit(c).filter(_ => false)
    ])(c));
};
//# sourceMappingURL=forms.js.map