import { h } from "@stencil/core";
import { fullBillingSchema } from "./billing-form-schema";
import { billingForm } from "../../../styles/parts";
import { Header3 } from "../../../ui-components";
import checkoutBillingStateOptions from "../../../utils/checkout-billing-state-options";
import { FormController } from "../../../ui-components/form/form";
import { checkoutStore, onChange } from "../../../store/checkout.store";
export class BillingFormFull {
    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(fullBillingSchema());
        const fields = checkoutStore.billingFormFields;
        if (fields && Object.keys(fields).some((k) => fields[k])) {
            this.formController.setInitialValues(fields);
        }
        this.formController.values.subscribe(values => this.billingInfo = Object.assign({}, values));
        this.formController.errors.subscribe(errors => {
            this.errors = Object.assign({}, errors);
        });
    }
    componentDidLoad() {
        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() {
        if (!this.formController) {
            return null;
        }
        const billingFormDefaultValue = this.formController.getInitialValues();
        return (h("div", { part: billingForm, class: "mt-4" }, h(Header3, { text: "Billing address", class: "fs-6 fw-bold lh-lg mb-4" }), h("form", null, h("fieldset", null, this.legend && (h("legend", null, this.legend)), h("div", { class: "row gy-3" }, h("div", { class: "col-12" }, h("form-control-text", { name: 'name', label: 'Full Name', defaultValue: billingFormDefaultValue.name, errorText: this.errors.name, inputHandler: this.inputHandler })), h("div", { class: "col-12" }, h("form-control-text", { name: 'address_line1', label: 'Street Address', defaultValue: billingFormDefaultValue.address_line1, errorText: this.errors.address_line1, inputHandler: this.inputHandler })), h("div", { class: "col-12" }, h("form-control-text", { name: 'address_line2', label: "Apartment, Suite, etc. (optional)", defaultValue: billingFormDefaultValue.address_line2, errorText: this.errors.address_line2, inputHandler: this.inputHandler })), h("div", { class: "col-12" }, h("form-control-text", { name: 'address_city', label: "City", defaultValue: billingFormDefaultValue.address_city, errorText: this.errors.address_city, inputHandler: this.inputHandler })), h("div", { class: "col-12" }, h("form-control-select", { name: 'address_state', label: 'State / Province', options: checkoutBillingStateOptions, defaultValue: billingFormDefaultValue.address_state, errorText: this.errors.address_state, inputHandler: this.inputHandler })), h("div", { class: "col-12" }, h("form-control-text", { name: 'address_postal_code', label: "Postal Code", defaultValue: billingFormDefaultValue.address_postal_code, errorText: this.errors.address_postal_code, inputHandler: this.inputHandler, maxLength: 7 })))))));
    }
    static get is() { return "billing-form-full"; }
    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": []
                }
            }
        };
    }
}
