import { h } from "@stencil/core";
/**
 * Form 表单组件
 * 由输入框、选择器、单选框、多选框等控件组成，配合表单校验
 */
export class Form {
    el;
    /** 表单数据对象 */
    model = {};
    /** 表单验证规则 */
    rules = {};
    /** 标签位置 */
    labelPosition = 'right';
    /** 标签宽度 */
    labelWidth = '80px';
    /** 是否行内表单 */
    inline = false;
    /** 表单尺寸 */
    size = 'default';
    /** 是否禁用 */
    disabled = false;
    fields = [];
    fieldRegistry = new Map();
    // 用 componentWillLoad 而非 componentDidLoad 注册监听器：
    // Stencil 生命周期里子组件(FormItem)的 componentDidLoad 先于父组件(Form)触发，
    // 若在 componentDidLoad 才挂监听，会错过子项派发的 hbFormFieldRegister，
    // 导致 fieldRegistry 为空、validate() 永远返回 true（真实浏览器里复现，mock-doc 被 waitForChanges 掩盖）。
    componentWillLoad() {
        this.el.addEventListener('hbFormFieldRegister', ((e) => {
            const { prop, validate, resetValue, getValue } = e.detail;
            this.fieldRegistry.set(prop, { prop, validate, resetValue, getValue });
        }));
    }
    /**
     * 验证整个表单
     * @returns 是否验证通过
     */
    validate = async () => {
        const results = await Promise.all(Array.from(this.fieldRegistry.values()).map(async (field) => {
            const errors = await field.validate();
            return errors.length === 0;
        }));
        return results.every(Boolean);
    };
    /**
     * 重置表单
     */
    resetFields = () => {
        this.fieldRegistry.forEach((field) => field.resetValue());
    };
    render() {
        return (h("form", { key: '233f774fd2e6a7331ce01d7b729c3a77dd5793a9', class: {
                'hb-form': true,
                [`hb-form--label-${this.labelPosition}`]: true,
                'hb-form--inline': this.inline,
            }, onSubmit: (e) => e.preventDefault(), novalidate: true }, h("slot", { key: '99055d1a239483acc153523aeb218ad420bd9610' })));
    }
    static get is() { return "hb-form"; }
    static get encapsulation() { return "shadow"; }
    static get originalStyleUrls() {
        return {
            "$": ["form.css"]
        };
    }
    static get styleUrls() {
        return {
            "$": ["form.css"]
        };
    }
    static get properties() {
        return {
            "model": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "Record<string, any>",
                    "resolved": "{ [x: string]: any; }",
                    "references": {
                        "Record": {
                            "location": "global",
                            "id": "global::Record"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "\u8868\u5355\u6570\u636E\u5BF9\u8C61"
                },
                "getter": false,
                "setter": false,
                "defaultValue": "{}"
            },
            "rules": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "Record<string, any[]>",
                    "resolved": "{ [x: string]: any[]; }",
                    "references": {
                        "Record": {
                            "location": "global",
                            "id": "global::Record"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "\u8868\u5355\u9A8C\u8BC1\u89C4\u5219"
                },
                "getter": false,
                "setter": false,
                "defaultValue": "{}"
            },
            "labelPosition": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "'left' | 'right' | 'top'",
                    "resolved": "\"left\" | \"right\" | \"top\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "\u6807\u7B7E\u4F4D\u7F6E"
                },
                "getter": false,
                "setter": false,
                "attribute": "label-position",
                "reflect": false,
                "defaultValue": "'right'"
            },
            "labelWidth": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "\u6807\u7B7E\u5BBD\u5EA6"
                },
                "getter": false,
                "setter": false,
                "attribute": "label-width",
                "reflect": false,
                "defaultValue": "'80px'"
            },
            "inline": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "\u662F\u5426\u884C\u5185\u8868\u5355"
                },
                "getter": false,
                "setter": false,
                "attribute": "inline",
                "reflect": false,
                "defaultValue": "false"
            },
            "size": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "'large' | 'default' | 'small'",
                    "resolved": "\"default\" | \"large\" | \"small\"",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "\u8868\u5355\u5C3A\u5BF8"
                },
                "getter": false,
                "setter": false,
                "attribute": "size",
                "reflect": false,
                "defaultValue": "'default'"
            },
            "disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "\u662F\u5426\u7981\u7528"
                },
                "getter": false,
                "setter": false,
                "attribute": "disabled",
                "reflect": false,
                "defaultValue": "false"
            },
            "validate": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "() => Promise<boolean>",
                    "resolved": "() => Promise<boolean>",
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [{
                            "name": "returns",
                            "text": "\u662F\u5426\u9A8C\u8BC1\u901A\u8FC7"
                        }],
                    "text": "\u9A8C\u8BC1\u6574\u4E2A\u8868\u5355"
                },
                "getter": false,
                "setter": false,
                "defaultValue": "async (): Promise<boolean> => {\n    const results = await Promise.all(\n      Array.from(this.fieldRegistry.values()).map(async (field) => {\n        const errors = await field.validate();\n        return errors.length === 0;\n      })\n    );\n    return results.every(Boolean);\n  }"
            },
            "resetFields": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "() => void",
                    "resolved": "() => void",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "\u91CD\u7F6E\u8868\u5355"
                },
                "getter": false,
                "setter": false,
                "defaultValue": "(): void => {\n    this.fieldRegistry.forEach((field) => field.resetValue());\n  }"
            }
        };
    }
    static get states() {
        return {
            "fields": {}
        };
    }
    static get elementRef() { return "el"; }
}
//# sourceMappingURL=Form.js.map
