import { h } from "@stencil/core";
import { PlatformServiceName, } from "@limetech/lime-web-components";
import { codes } from "currency-codes";
import { isNonNull } from "../../util/non-null";
/**
 * All currencies have a ISO code, an international standard with three-letters
 * which is used by many systems such as ERPs.
 *
 * When the CRM is integrated with a 3rd-party system,
 * CRM end-users are supposed to exactly know the correct ISO codes of currencies
 * and type them correctly, to make sure that the integration work flawlessly.
 * For example, a currency field may be presented on the object cards of "Invoice" or "Order".
 * This could be hard task and prone to errors.
 *
 * To prevent errors, this component can be used instead of
 * a basic input field with verifications,
 * and ease the job both for you and the end-users.
 *
 * :::tip
 * You can either use one of the currency presets provided by the component,
 * or provide your own array of currencies with `secondaryText` as the name of
 * the currency, `text` as the 3-letters ISO code, and `icon` as symbol of the
 * currency.
 * :::
 *
 * Our preset currency lists are as follows, and they are all
 * alphabetically sorted.
 *
 * @exampleComponent limebb-example-currency-picker
 * @exampleComponent limebb-example-currency-picker-nordic
 * @exampleComponent limebb-example-currency-picker-european
 */
export class CurrencyPicker {
    constructor() {
        /**
         * The list of currencies. If currencies are not provided it will default to `allCurrencies`,
         * We also support ´nordicMarketCurrencies´ and ´europeanMarketCurrencies´.
         */
        this.currencies = codes();
        /**
         * Whether the current property is required
         */
        this.required = false;
        /**
         * Whether the current property is readonly
         */
        this.readonly = false;
        /**
         * Whether the current property is invalid
         */
        this.invalid = false;
        /**
         * Whether the current property is disabled
         */
        this.disabled = false;
        this.search = async (query) => {
            if (query === '') {
                return this.currencies.map(this.createListItem).filter(isNonNull);
            }
            return this.currencies
                .map(this.createListItem)
                .filter(isNonNull)
                .filter((item) => {
                var _a;
                return (((_a = item === null || item === void 0 ? void 0 : item.secondaryText) === null || _a === void 0 ? void 0 : _a.toLowerCase().includes(query.toLowerCase())) ||
                    item.text.toLowerCase().includes(query.toLowerCase()));
            });
        };
        this.createListItem = (currency) => {
            if (!currency) {
                return;
            }
            return {
                text: currency,
                value: currency,
                secondaryText: this.getCurrencyName(currency),
                icon: this.getCurrencyIcon(currency),
            };
        };
        this.handleChange = (event) => {
            var _a;
            event.stopPropagation();
            this.change.emit((_a = event.detail) === null || _a === void 0 ? void 0 : _a.value);
        };
    }
    render() {
        return (h("limel-picker", { key: '2e9dcd3bf98fe7a3377aa24c0e45814f2c698633', label: this.label || this.defaultLabel, value: this.createListItem(this.value), helperText: this.helperText, onChange: this.handleChange, badgeIcons: false, disabled: this.disabled, required: this.required, readonly: this.readonly, invalid: this.readonly, searcher: this.search }));
    }
    getCurrencyName(code) {
        var _a;
        const formatter = Intl.NumberFormat(this.application.getLanguage(), {
            style: 'currency',
            currency: code,
            currencyDisplay: 'name',
        });
        try {
            return (_a = formatter
                .formatToParts(1)
                .find((part) => part.type === 'currency')) === null || _a === void 0 ? void 0 : _a.value;
        }
        catch (_b) {
            return;
        }
    }
    getCurrencyIcon(currency) {
        return {
            EUR: 'euro',
            USD: 'us_dollar',
            SEK: 'sek',
            NOK: 'sek',
            DKK: 'sek',
            ISK: 'sek',
        }[currency];
    }
    get translator() {
        return this.platform.get(PlatformServiceName.Translate);
    }
    get application() {
        return this.platform.get(PlatformServiceName.Application);
    }
    get defaultLabel() {
        return this.translator.get('webclient.currency-picker.label');
    }
    static get is() { return "limebb-currency-picker"; }
    static get encapsulation() { return "shadow"; }
    static get properties() {
        return {
            "platform": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "LimeWebComponentPlatform",
                    "resolved": "LimeWebComponentPlatform",
                    "references": {
                        "LimeWebComponentPlatform": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::LimeWebComponentPlatform",
                            "referenceLocation": "LimeWebComponentPlatform"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false
            },
            "context": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "LimeWebComponentContext",
                    "resolved": "LimeWebComponentContext",
                    "references": {
                        "LimeWebComponentContext": {
                            "location": "import",
                            "path": "@limetech/lime-web-components",
                            "id": "node_modules::LimeWebComponentContext",
                            "referenceLocation": "LimeWebComponentContext"
                        }
                    }
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": ""
                },
                "getter": false,
                "setter": false
            },
            "label": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The label of the currency picker, if not specified it takes the word \"Currency\"\nas a default with translations for our supported languages."
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "label"
            },
            "currencies": {
                "type": "unknown",
                "mutable": false,
                "complexType": {
                    "original": "string[]",
                    "resolved": "string[]",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The list of currencies. If currencies are not provided it will default to `allCurrencies`,\nWe also support \u00B4nordicMarketCurrencies\u00B4 and \u00B4europeanMarketCurrencies\u00B4."
                },
                "getter": false,
                "setter": false,
                "defaultValue": "codes()"
            },
            "helperText": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string | undefined",
                    "references": {}
                },
                "required": false,
                "optional": true,
                "docs": {
                    "tags": [],
                    "text": "The helper text for the current property"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "helper-text"
            },
            "required": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Whether the current property is required"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "required",
                "defaultValue": "false"
            },
            "readonly": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Whether the current property is readonly"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "readonly",
                "defaultValue": "false"
            },
            "invalid": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Whether the current property is invalid"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "invalid",
                "defaultValue": "false"
            },
            "disabled": {
                "type": "boolean",
                "mutable": false,
                "complexType": {
                    "original": "boolean",
                    "resolved": "boolean",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "Whether the current property is disabled"
                },
                "getter": false,
                "setter": false,
                "reflect": true,
                "attribute": "disabled",
                "defaultValue": "false"
            },
            "value": {
                "type": "string",
                "mutable": false,
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                },
                "required": false,
                "optional": false,
                "docs": {
                    "tags": [],
                    "text": "The value of the current property"
                },
                "getter": false,
                "setter": false,
                "reflect": false,
                "attribute": "value"
            }
        };
    }
    static get events() {
        return [{
                "method": "change",
                "name": "change",
                "bubbles": true,
                "cancelable": true,
                "composed": true,
                "docs": {
                    "tags": [],
                    "text": "The event to emit when the value of the current property has changed"
                },
                "complexType": {
                    "original": "string",
                    "resolved": "string",
                    "references": {}
                }
            }];
    }
}
