"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var icache_1 = require("@dojo/framework/core/middleware/icache");
var theme_1 = require("../middleware/theme");
var vdom_1 = require("@dojo/framework/core/vdom");
var css = require("../theme/default/form.m.css");
var middleware_1 = require("./middleware");
var form = middleware_1.default();
function valueEqual(a, b) {
    if (b === void 0) { b = {}; }
    return (Object.keys(a).length === Object.keys(b).length &&
        Object.keys(a).every(function (key) { return a[key] === b[key]; }));
}
function isSubmitForm(properties) {
    return properties.onSubmit !== undefined;
}
function isActionForm(properties) {
    return properties.action !== undefined;
}
var factory = vdom_1.create({ form: form, theme: theme_1.default, icache: icache_1.default })
    .properties()
    .children();
exports.default = factory(function Form(_a) {
    var properties = _a.properties, children = _a.children, _b = _a.middleware, form = _b.form, theme = _b.theme, icache = _b.icache;
    var themedCss = theme.classes(css);
    var props = properties();
    var formProps = {
        classes: [theme.variant(), themedCss.root],
        name: props.name
    };
    var initialValue = props.initialValue, value = props.value, onValue = props.onValue;
    if (isSubmitForm(props)) {
        formProps = tslib_1.__assign({}, formProps, { onsubmit: function (event) {
                event.preventDefault();
                form.submit(function (value) { return props.onSubmit(value); });
            } });
    }
    else if (isActionForm(props)) {
        var action = props.action, _c = props.method, method = _c === void 0 ? 'post' : _c;
        formProps = tslib_1.__assign({}, formProps, { action: action,
            method: method });
    }
    var _d = tslib_1.__read(children(), 1), renderer = _d[0];
    onValue && form.onValue(onValue);
    if (value) {
        form.value(value);
    }
    else if (initialValue !== undefined && !valueEqual(initialValue, icache.get('initial'))) {
        icache.set('initial', initialValue, false);
        form.value(initialValue);
    }
    return vdom_1.tsx("form", tslib_1.__assign({}, formProps), renderer(form));
});
var formGroupFactory = vdom_1.create({ theme: theme_1.default })
    .properties()
    .children();
exports.FormGroup = formGroupFactory(function FormRow(_a) {
    var properties = _a.properties, children = _a.children, theme = _a.middleware.theme;
    var column = properties().column;
    var themedCss = theme.classes(css);
    return (vdom_1.tsx("div", { key: "root", classes: [
            theme.variant(),
            themedCss.groupRoot,
            !column && themedCss.row,
            column && themedCss.column
        ] }, children()));
});
var formFieldFactory = vdom_1.create({ theme: theme_1.default });
exports.FormField = formFieldFactory(function FormField(_a) {
    var children = _a.children, theme = _a.middleware.theme;
    var themedCss = theme.classes(css);
    return (vdom_1.tsx("div", { key: "root", classes: [theme.variant(), themedCss.fieldRoot] }, children()));
});

/*# sourceMappingURL=index.js.map*/