1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef ECMASCRIPT_JS_PLURAL_RULES_H
17#define ECMASCRIPT_JS_PLURAL_RULES_H
18
19#include "ecmascript/js_array.h"
20#include "ecmascript/js_hclass.h"
21#include "ecmascript/js_intl.h"
22#include "ecmascript/js_locale.h"
23#include "ecmascript/js_object.h"
24#include "ecmascript/js_tagged_value.h"
25
26namespace panda::ecmascript {
27constexpr int MNFD_DEFAULT = 0;
28constexpr int MXFD_DEFAULT = 3;
29
30class JSPluralRules : public JSObject {
31public:
32    CAST_CHECK(JSPluralRules, IsJSPluralRules);
33
34    static constexpr size_t LOCALE_OFFSET = JSObject::SIZE;
35    ACCESSORS(Locale, LOCALE_OFFSET, MINIMUM_INTEGER_DIGITS_OFFSET)
36    ACCESSORS(MinimumIntegerDigits, MINIMUM_INTEGER_DIGITS_OFFSET, MINIMUM_FRACTION_DIGITS_OFFSET)
37    ACCESSORS(MinimumFractionDigits, MINIMUM_FRACTION_DIGITS_OFFSET, MAXIMUM_FRACTION_DIGITS_OFFSET)
38    ACCESSORS(MaximumFractionDigits, MAXIMUM_FRACTION_DIGITS_OFFSET, MININUM_SIGNIFICANT_DIGITS_OFFSET)
39    ACCESSORS(MinimumSignificantDigits, MININUM_SIGNIFICANT_DIGITS_OFFSET, MAXINUM_SIGNIFICANT_DIGITS_OFFSET)
40    ACCESSORS(MaximumSignificantDigits, MAXINUM_SIGNIFICANT_DIGITS_OFFSET, ICU_PLURAL_RULES_OFFSET)
41    ACCESSORS(IcuPR, ICU_PLURAL_RULES_OFFSET, ICU_NUMBER_FORMAT_OFFSET)
42    ACCESSORS(IcuNF, ICU_NUMBER_FORMAT_OFFSET, BIT_FIELD_OFFSET)
43    ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
44    DEFINE_ALIGN_SIZE(LAST_OFFSET);
45
46    // define BitField
47    static constexpr size_t ROUNDING_TYPE_BITS = 3;
48    static constexpr size_t TYPE_BITS = 2;
49    FIRST_BIT_FIELD(BitField, RoundingType, RoundingType, ROUNDING_TYPE_BITS)
50    NEXT_BIT_FIELD(BitField, Type, TypeOption, TYPE_BITS, RoundingType)
51
52    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET)
53    DECL_DUMP()
54
55    icu::number::LocalizedNumberFormatter *GetIcuNumberFormatter() const;
56
57    static void SetIcuNumberFormatter(JSThread *thread, const JSHandle<JSPluralRules> &pluralRules,
58        const icu::number::LocalizedNumberFormatter &icuNF, const NativePointerCallback &callback);
59
60    static void FreeIcuNumberFormatter(void *env, void *pointer, void* hint = nullptr);
61
62    icu::PluralRules *GetIcuPluralRules() const;
63
64    static void SetIcuPluralRules(JSThread *thread, const JSHandle<JSPluralRules> &pluralRules,
65        const icu::PluralRules &icuPR, const NativePointerCallback &callback);
66
67    static void FreeIcuPluralRules(void *env, void *pointer, void* hint = nullptr);
68
69    static JSHandle<TaggedArray> BuildLocaleSet(JSThread *thread, const std::set<std::string> &icuAvailableLocales);
70
71    static JSHandle<TaggedArray> GetAvailableLocales(JSThread *thread);
72
73    // 15.1.1 InitializePluralRules ( pluralRules, locales, options )
74    static JSHandle<JSPluralRules> InitializePluralRules(JSThread *thread, const JSHandle<JSPluralRules> &pluralRules,
75                                                         const JSHandle<JSTaggedValue> &locales,
76                                                         const JSHandle<JSTaggedValue> &options);
77
78    // 15.1.4 ResolvePlural ( pluralRules, n )
79    static JSHandle<EcmaString> ResolvePlural(JSThread *thread, const JSHandle<JSPluralRules> &pluralRules, double n);
80
81    static void ResolvedOptions(JSThread *thread, const JSHandle<JSPluralRules> &pluralRules,
82                                const JSHandle<JSObject> &options);
83};
84}  // namespace panda::ecmascript
85#endif  // ECMASCRIPT_JS_PLURAL_RULES_H