/*!
 * Built by ShiftSoftware
 * Copyright (c)
 */
import { h } from "@stencil/core";
import { VehicleImageViewer } from "../../form-elements/VehicleImageViewer";
import { CalendarDaysIcon } from "../../../global/assets/calendar-days-icon";
import { format, isBefore, isEqual } from "date-fns";
import { decodeTimeOffset } from "../../../global/lib/decode-time-offset";
import { populateItems } from "../../../global/lib/populate-items";
export const getFormMappers = (extraMappers = {}) => (Object.assign({ submit: ({ props }) => h("form-submit", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props)), inputPreview: ({ props }) => h("form-input-preview", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props)), fileUploader: ({ props }) => h("form-file", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props)), formStepper: ({ props, language }) => h("form-stepper", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name, language: language }, props)), back: ({ props }) => h("form-stepper-control", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props)), stepperSubmit: ({ props }) => h("form-stepper-submit", Object.assign({ key: (props === null || props === void 0 ? void 0 : props.step) || (props === null || props === void 0 ? void 0 : props.name) }, props)), name: ({ props }) => h("form-input", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props)), lastName: ({ props }) => h("form-input", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props)), email: ({ props }) => h("form-input", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name, type: "email" }, props)), message: ({ props }) => h("form-text-area", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props)), file: ({ props }) => h("form-file", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props)), vin: ({ props }) => h("form-vin-input", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props)), vehicleImage: ({ form }) => h(VehicleImageViewer, { form: form }), phone: ({ props, isLoading }) => h("form-phone-number", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name, isLoading: isLoading }, props)), vehicle: ({ language, props }) => {
        const fetcher = async ({ signal, context }) => {
            var _a, _b;
            const params = new URLSearchParams(window.location.search);
            let paramValue = (_a = (params.get(props === null || props === void 0 ? void 0 : props.vehicleIdQueryParam) || '')) === null || _a === void 0 ? void 0 : _a.toLowerCase();
            const vehicleEndpoint = props.vehicleApi;
            const response = await fetch(vehicleEndpoint, { signal, headers: { 'Accept-Language': language } });
            let options;
            let defaultIndex = -1;
            if (props === null || props === void 0 ? void 0 : props.dynamic) {
                const res = await response.json();
                options = populateItems(res, props, true);
                options.forEach((vehicle, idx) => {
                    var _a, _b;
                    if (!props.defaultValue && paramValue && (((_a = vehicle === null || vehicle === void 0 ? void 0 : vehicle.label) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === paramValue || ((_b = `${vehicle.value}`) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === paramValue))
                        defaultIndex = idx;
                });
            }
            else if (props === null || props === void 0 ? void 0 : props.vehiclesApiStrapiFormat) {
                const res = await response.json();
                options = res.data;
                options = options.map((vehicle, idx) => {
                    var _a, _b, _c, _d, _e;
                    if (!props.defaultValue && paramValue && (((_a = vehicle === null || vehicle === void 0 ? void 0 : vehicle.GradeName) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === paramValue || ((_b = `${vehicle.id}`) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === paramValue))
                        defaultIndex = idx;
                    return {
                        label: vehicle.attributes.GradeName,
                        value: (props === null || props === void 0 ? void 0 : props.useNamedValue) ? `${(vehicle === null || vehicle === void 0 ? void 0 : vehicle.GradeName) || ((_c = vehicle === null || vehicle === void 0 ? void 0 : vehicle.attributes) === null || _c === void 0 ? void 0 : _c.GradeName)}` : `${vehicle.id}`,
                        meta: Object.assign(Object.assign({}, vehicle), { image: ((_d = vehicle === null || vehicle === void 0 ? void 0 : vehicle.Cover) === null || _d === void 0 ? void 0 : _d.url) || ((_e = vehicle.attributes.Cover.data.attributes) === null || _e === void 0 ? void 0 : _e.url) }),
                    };
                });
            }
            else {
                options = await response.json();
                options = options.map((vehicle, idx) => {
                    var _a, _b;
                    if (!props.defaultValue && paramValue && (((_a = vehicle === null || vehicle === void 0 ? void 0 : vehicle.Title) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === paramValue || ((_b = `${vehicle.ID}`) === null || _b === void 0 ? void 0 : _b.toLowerCase()) === paramValue))
                        defaultIndex = idx;
                    return {
                        label: vehicle.Title,
                        value: (props === null || props === void 0 ? void 0 : props.useNamedValue) ? `${vehicle.Title}` : `${vehicle.ID}`,
                        meta: Object.assign(Object.assign({}, vehicle), { image: vehicle.Image }),
                    };
                });
            }
            if (!(props === null || props === void 0 ? void 0 : props.defaultValue)) {
                if (defaultIndex > -1)
                    context.defaultValue = (_b = options[defaultIndex]) === null || _b === void 0 ? void 0 : _b.value;
                else
                    context.defaultValue = '';
            }
            else
                context.defaultValue = props === null || props === void 0 ? void 0 : props.defaultValue;
            return options;
        };
        return h("form-select", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props, { searchable: true, fetcher: fetcher, language: language }));
    }, companyBranchId: ({ language, props }) => {
        const fetcher = async ({ signal }) => {
            const dealerEndpoint = props === null || props === void 0 ? void 0 : props.branchApi;
            const response = await fetch(dealerEndpoint, { signal, headers: { 'Accept-Language': language } });
            const options = (await response.json()).map(dealer => ({
                label: dealer.Name,
                value: `${dealer.ID}`,
                meta: Object.assign({}, dealer),
            }));
            return options;
        };
        return h("form-select", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props, { clearable: true, searchable: true, fetcher: fetcher, language: language }));
    }, vacancyId: ({ language, props }) => {
        const fetcher = async ({ signal }) => {
            const dealerEndpoint = props === null || props === void 0 ? void 0 : props.vacancyApi;
            const response = await fetch(dealerEndpoint, { signal, headers: { 'Accept-Language': language } });
            const options = (await response.json()).map(dealer => ({
                label: dealer.Title,
                value: `${dealer.ID}`,
                meta: Object.assign({}, dealer),
            }));
            return options;
        };
        return h("form-select", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props, { clearable: true, searchable: true, fetcher: fetcher, language: language }));
    }, cityId: ({ language, props }) => {
        const fetcher = async ({ signal }) => {
            const dealerEndpoint = props === null || props === void 0 ? void 0 : props.cityApi;
            const response = await fetch(dealerEndpoint, { signal, headers: { 'Accept-Language': language } });
            const options = (await response.json()).map(dealer => ({
                label: dealer.Name,
                value: `${dealer.ID}`,
                meta: Object.assign({}, dealer),
            }));
            return options;
        };
        return h("form-select", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props, { clearable: true, searchable: true, fetcher: fetcher, language: language }));
    }, date: ({ props }) => h("form-picker-input", Object.assign({ type: "date" }, props, { icon: h(CalendarDaysIcon, null) })), time: ({ language, props }) => {
        const fetcher = async () => {
            const options = [];
            if (Array.isArray(props.span) && Array.isArray(props.min) && Array.isArray(props.max) && props.format) {
                let tempDate = decodeTimeOffset({ offsets: props.min });
                let maxDate = decodeTimeOffset({ offsets: props.max });
                while (isBefore(tempDate, maxDate) || isEqual(tempDate, maxDate)) {
                    options.push({
                        value: format(tempDate, props.format),
                        label: format(tempDate, props.format),
                    });
                    tempDate = decodeTimeOffset({ offsets: props.span, date: tempDate });
                }
            }
            return options;
        };
        return h("form-select", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props, { clearable: true, fetcher: fetcher, language: language }));
    }, year: ({ language, props }) => {
        const minYear = (props === null || props === void 0 ? void 0 : props.min) || new Date().getFullYear() - 20;
        const maxYear = (props === null || props === void 0 ? void 0 : props.max) || new Date().getFullYear();
        const options = [];
        for (let year = minYear; year <= maxYear; year++) {
            options.push({
                value: `${year}`,
                label: `${year}`,
            });
        }
        const fetcher = async ({ language }) => {
            var _a, _b, _c, _d, _e, _f, _g, _h;
            let parsedOptions = [...options];
            if (props === null || props === void 0 ? void 0 : props.firstOption) {
                parsedOptions = [{ label: ((_b = (_a = props === null || props === void 0 ? void 0 : props.firstOption) === null || _a === void 0 ? void 0 : _a.label) === null || _b === void 0 ? void 0 : _b[language]) || '', value: ((_d = (_c = props === null || props === void 0 ? void 0 : props.firstOption) === null || _c === void 0 ? void 0 : _c.value) === null || _d === void 0 ? void 0 : _d[language]) || '' }, ...parsedOptions];
            }
            if (props === null || props === void 0 ? void 0 : props.lastOption) {
                parsedOptions = [...parsedOptions, { label: ((_f = (_e = props === null || props === void 0 ? void 0 : props.lastOption) === null || _e === void 0 ? void 0 : _e.label) === null || _f === void 0 ? void 0 : _f[language]) || '', value: ((_h = (_g = props === null || props === void 0 ? void 0 : props.lastOption) === null || _g === void 0 ? void 0 : _g.value) === null || _h === void 0 ? void 0 : _h[language]) || '' }];
            }
            return parsedOptions;
        };
        return h("form-select", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props, { clearable: true, fetcher: fetcher, language: language }));
    }, currentVehicleBrand: ({ form, language, props, locale }) => {
        const fetcher = async ({ signal }) => {
            var _a;
            const currentVehiclesEndpoint = (_a = form.context.structure) === null || _a === void 0 ? void 0 : _a.data.currentVehiclesApi;
            const response = await fetch(currentVehiclesEndpoint, { signal, headers: { 'Accept-Language': 'en' } });
            const options = (await response.json()).filter(vehicle => vehicle.name !== 'Other');
            return [
                ...options.map(vehicle => ({
                    label: vehicle.Name,
                    value: `${vehicle.ID}`,
                    meta: Object.assign({}, vehicle),
                })),
                {
                    value: 'Other',
                    label: locale.Other,
                },
            ];
        };
        return h("form-select", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props, { searchable: true, fetcher: fetcher, language: language }));
    }, currentVehicleModel: ({ form, language, props, locale }) => {
        form.addWatcher('currentVehicleBrand');
        const currentVehicleBrand = form === null || form === void 0 ? void 0 : form.getValue('currentVehicleBrand');
        const fetcher = async ({}) => {
            var _a, _b;
            if (!currentVehicleBrand)
                return [];
            const selectedBrand = (_b = (_a = form.context['currentVehicleBrandList']) === null || _a === void 0 ? void 0 : _a.find(vehicle => vehicle.value === currentVehicleBrand)) === null || _b === void 0 ? void 0 : _b.meta;
            if (!selectedBrand)
                return [];
            return [
                ...selectedBrand === null || selectedBrand === void 0 ? void 0 : selectedBrand.Models.map(model => ({ label: model.Name, value: `${model.ID}` })),
                {
                    value: 'Other',
                    label: locale.Other,
                },
            ];
        };
        return (h("form-select", Object.assign({}, props, { searchable: true, key: props === null || props === void 0 ? void 0 : props.name, fetcher: fetcher, language: language, resetKey: currentVehicleBrand, fetcherKey: currentVehicleBrand, isRequired: currentVehicleBrand !== 'Other', isDisabled: !currentVehicleBrand || currentVehicleBrand === 'Other' })));
    }, ownVehicle: ({ language, props }) => {
        const fetcher = async ({}) => {
            var _a, _b, _c, _d;
            return [
                {
                    value: 'yes',
                    label: (_b = (_a = props === null || props === void 0 ? void 0 : props.localization) === null || _a === void 0 ? void 0 : _a[language]) === null || _b === void 0 ? void 0 : _b.yes,
                },
                {
                    value: 'no',
                    label: (_d = (_c = props === null || props === void 0 ? void 0 : props.localization) === null || _c === void 0 ? void 0 : _c[language]) === null || _d === void 0 ? void 0 : _d.no,
                },
            ];
        };
        return h("form-select", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props, { fetcher: fetcher, language: language }));
    }, conditionalCurrentVehicleBrand: ({ form, language, props, locale }) => {
        form.addWatcher('ownVehicle');
        const ownVehicle = (form === null || form === void 0 ? void 0 : form.getValue('ownVehicle')) === 'yes';
        const fetcher = async ({ signal }) => {
            const currentVehiclesEndpoint = props === null || props === void 0 ? void 0 : props.brandApi;
            const response = await fetch(currentVehiclesEndpoint, { signal, headers: { 'Accept-Language': 'en' } });
            const options = (await response.json()).filter(vehicle => vehicle.name !== 'Other');
            return [
                ...options.map(vehicle => ({
                    label: vehicle.Name,
                    value: (props === null || props === void 0 ? void 0 : props.useNamedValue) ? vehicle.Name : `${vehicle.ID}`,
                    meta: Object.assign({}, vehicle),
                })),
                {
                    value: 'Other',
                    label: locale.Other,
                },
            ];
        };
        return h("form-select", Object.assign({ key: props === null || props === void 0 ? void 0 : props.name }, props, { searchable: true, fetcher: fetcher, language: language, resetKey: ownVehicle, isRequired: ownVehicle, isDisabled: !ownVehicle }));
    }, conditionalCurrentVehicleModel: ({ form, language, props, locale }) => {
        form.addWatcher('ownVehicle');
        form.addWatcher('conditionalCurrentVehicleBrand');
        const ownVehicle = (form === null || form === void 0 ? void 0 : form.getValue('ownVehicle')) === 'yes';
        const currentVehicleBrand = form === null || form === void 0 ? void 0 : form.getValue('conditionalCurrentVehicleBrand');
        const fetcher = async ({}) => {
            var _a, _b;
            if (!currentVehicleBrand)
                return [];
            const selectedBrand = (_b = (_a = form.context['conditionalCurrentVehicleBrandList']) === null || _a === void 0 ? void 0 : _a.find(vehicle => vehicle.value === currentVehicleBrand)) === null || _b === void 0 ? void 0 : _b.meta;
            if (!selectedBrand)
                return [];
            return [
                ...selectedBrand === null || selectedBrand === void 0 ? void 0 : selectedBrand.Models.map(model => ({ label: model.Name, value: (props === null || props === void 0 ? void 0 : props.useNamedValue) ? model.Name : `${model.ID}` })),
                {
                    value: 'Other',
                    label: locale.Other,
                },
            ];
        };
        return (h("form-select", Object.assign({}, props, { searchable: true, fetcher: fetcher, key: props === null || props === void 0 ? void 0 : props.name, language: language, resetKey: currentVehicleBrand, fetcherKey: currentVehicleBrand, isRequired: ownVehicle && currentVehicleBrand !== 'Other', isDisabled: !currentVehicleBrand || currentVehicleBrand === 'Other' })));
    } }, extraMappers));
