import { h } from "@stencil/core";
import { nameOnlySchema } from "./billing-form-schema";
import { billingForm } from "../../../styles/parts";
import { FormController } from "../../../ui-components/form/form";
import { checkoutStore, onChange } from "../../../store/checkout.store";
export class BankAccountBillingFormSimple {
    constructor() {
        this.errors = {};
        this.inputHandler = (name, value) => {
            this.formController.setValues(Object.assign(Object.assign({}, this.formController.values.getValue()), { [name]: value }));
        };
    }
    componentWillLoad() {
        this.formController = new FormController(nameOnlySchema());
        const fields = checkoutStore.billingFormFields;
        if (fields && Object.keys(fields).some((k) => fields[k])) {
            this.formController.setInitialValues(fields);
        }
    }
    componentDidLoad() {
        this.formController.values.subscribe(values => this.billingInfo = Object.assign({}, values));
        this.formController.errors.subscribe(errors => {
            this.errors = Object.assign({}, errors);
        });
        this.unsubscribeFromStore = onChange('billingFormFields', (newValue) => {
            this.formController.setInitialValues(newValue);
        });
    }
    disconnectedCallback() {
        var _a;
        (_a = this.unsubscribeFromStore) === null || _a === void 0 ? void 0 : _a.call(this);
    }
    async getValues() {
        return this.formController.values.getValue();
    }
    async validate() {
        const isValid = await this.formController.validate();
        const errors = this.formController.errors.getValue();
        return { isValid, errors };
    }
    render() {
        const billingFormDefaultValue = this.formController.getInitialValues();
        return (h("div", { key: 'e8c53c98793d0296d69799c8449d762084d9859b', part: billingForm, class: "mt-4" }, h("form", { key: '83e59bb7bdcf4a3e0b4ed4320358796eae91726f' }, h("fieldset", { key: 'd92a4a02c531a08a270562701bc71440add5b545' }, this.legend && (h("legend", { key: '74ac1eb2c6fc629323f31be515e3aa204ae302a2' }, this.legend)), h("div", { key: '161888eb0680a4c9d6280440961f2926b08ac1ed', class: "row gy-3" }, h("div", { key: '9f6c899b7a4ff25a5eba5278bffe7d4e86eb00df', class: "col-12" }, h("form-control-text", { key: '74a9958fb74de730067fe4d87dfac33c12522169', name: 'name', label: 'Full Name', defaultValue: billingFormDefaultValue.name, errorText: this.errors.name, inputHandler: this.inputHandler })))))));
    }
    static get is() { return "bank-account-billing-form-simple"; }
    static get properties() {
        return {
            "legend": {
                "type": "string",
                "mutable": true,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "legend"
            }
        };
    }
    static get states() {
        return {
            "formController": {},
            "billingInfo": {},
            "errors": {}
        };
    }
    static get methods() {
        return {
            "getValues": {
                "complexType": {
                    "signature": "() => Promise<BillingFormFields>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        },
                        "BillingFormFields": {
                            "location": "import",
                            "path": "./billing-form-schema",
                            "id": "src/components/checkout/billing-form/billing-form-schema.ts::BillingFormFields"
                        }
                    },
                    "return": "Promise<BillingFormFields>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            },
            "validate": {
                "complexType": {
                    "signature": "() => Promise<{ isValid: boolean; errors: any; }>",
                    "parameters": [],
                    "references": {
                        "Promise": {
                            "location": "global",
                            "id": "global::Promise"
                        }
                    },
                    "return": "Promise<{ isValid: boolean; errors: any; }>"
                },
                "docs": {
                    "text": "",
                    "tags": []
                }
            }
        };
    }
}
