14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_JS_PLURAL_RULES_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_JS_PLURAL_RULES_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/js_array.h"
204514f5e3Sopenharmony_ci#include "ecmascript/js_hclass.h"
214514f5e3Sopenharmony_ci#include "ecmascript/js_intl.h"
224514f5e3Sopenharmony_ci#include "ecmascript/js_locale.h"
234514f5e3Sopenharmony_ci#include "ecmascript/js_object.h"
244514f5e3Sopenharmony_ci#include "ecmascript/js_tagged_value.h"
254514f5e3Sopenharmony_ci
264514f5e3Sopenharmony_cinamespace panda::ecmascript {
274514f5e3Sopenharmony_ciconstexpr int MNFD_DEFAULT = 0;
284514f5e3Sopenharmony_ciconstexpr int MXFD_DEFAULT = 3;
294514f5e3Sopenharmony_ci
304514f5e3Sopenharmony_ciclass JSPluralRules : public JSObject {
314514f5e3Sopenharmony_cipublic:
324514f5e3Sopenharmony_ci    CAST_CHECK(JSPluralRules, IsJSPluralRules);
334514f5e3Sopenharmony_ci
344514f5e3Sopenharmony_ci    static constexpr size_t LOCALE_OFFSET = JSObject::SIZE;
354514f5e3Sopenharmony_ci    ACCESSORS(Locale, LOCALE_OFFSET, MINIMUM_INTEGER_DIGITS_OFFSET)
364514f5e3Sopenharmony_ci    ACCESSORS(MinimumIntegerDigits, MINIMUM_INTEGER_DIGITS_OFFSET, MINIMUM_FRACTION_DIGITS_OFFSET)
374514f5e3Sopenharmony_ci    ACCESSORS(MinimumFractionDigits, MINIMUM_FRACTION_DIGITS_OFFSET, MAXIMUM_FRACTION_DIGITS_OFFSET)
384514f5e3Sopenharmony_ci    ACCESSORS(MaximumFractionDigits, MAXIMUM_FRACTION_DIGITS_OFFSET, MININUM_SIGNIFICANT_DIGITS_OFFSET)
394514f5e3Sopenharmony_ci    ACCESSORS(MinimumSignificantDigits, MININUM_SIGNIFICANT_DIGITS_OFFSET, MAXINUM_SIGNIFICANT_DIGITS_OFFSET)
404514f5e3Sopenharmony_ci    ACCESSORS(MaximumSignificantDigits, MAXINUM_SIGNIFICANT_DIGITS_OFFSET, ICU_PLURAL_RULES_OFFSET)
414514f5e3Sopenharmony_ci    ACCESSORS(IcuPR, ICU_PLURAL_RULES_OFFSET, ICU_NUMBER_FORMAT_OFFSET)
424514f5e3Sopenharmony_ci    ACCESSORS(IcuNF, ICU_NUMBER_FORMAT_OFFSET, BIT_FIELD_OFFSET)
434514f5e3Sopenharmony_ci    ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
444514f5e3Sopenharmony_ci    DEFINE_ALIGN_SIZE(LAST_OFFSET);
454514f5e3Sopenharmony_ci
464514f5e3Sopenharmony_ci    // define BitField
474514f5e3Sopenharmony_ci    static constexpr size_t ROUNDING_TYPE_BITS = 3;
484514f5e3Sopenharmony_ci    static constexpr size_t TYPE_BITS = 2;
494514f5e3Sopenharmony_ci    FIRST_BIT_FIELD(BitField, RoundingType, RoundingType, ROUNDING_TYPE_BITS)
504514f5e3Sopenharmony_ci    NEXT_BIT_FIELD(BitField, Type, TypeOption, TYPE_BITS, RoundingType)
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ci    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET)
534514f5e3Sopenharmony_ci    DECL_DUMP()
544514f5e3Sopenharmony_ci
554514f5e3Sopenharmony_ci    icu::number::LocalizedNumberFormatter *GetIcuNumberFormatter() const;
564514f5e3Sopenharmony_ci
574514f5e3Sopenharmony_ci    static void SetIcuNumberFormatter(JSThread *thread, const JSHandle<JSPluralRules> &pluralRules,
584514f5e3Sopenharmony_ci        const icu::number::LocalizedNumberFormatter &icuNF, const NativePointerCallback &callback);
594514f5e3Sopenharmony_ci
604514f5e3Sopenharmony_ci    static void FreeIcuNumberFormatter(void *env, void *pointer, void* hint = nullptr);
614514f5e3Sopenharmony_ci
624514f5e3Sopenharmony_ci    icu::PluralRules *GetIcuPluralRules() const;
634514f5e3Sopenharmony_ci
644514f5e3Sopenharmony_ci    static void SetIcuPluralRules(JSThread *thread, const JSHandle<JSPluralRules> &pluralRules,
654514f5e3Sopenharmony_ci        const icu::PluralRules &icuPR, const NativePointerCallback &callback);
664514f5e3Sopenharmony_ci
674514f5e3Sopenharmony_ci    static void FreeIcuPluralRules(void *env, void *pointer, void* hint = nullptr);
684514f5e3Sopenharmony_ci
694514f5e3Sopenharmony_ci    static JSHandle<TaggedArray> BuildLocaleSet(JSThread *thread, const std::set<std::string> &icuAvailableLocales);
704514f5e3Sopenharmony_ci
714514f5e3Sopenharmony_ci    static JSHandle<TaggedArray> GetAvailableLocales(JSThread *thread);
724514f5e3Sopenharmony_ci
734514f5e3Sopenharmony_ci    // 15.1.1 InitializePluralRules ( pluralRules, locales, options )
744514f5e3Sopenharmony_ci    static JSHandle<JSPluralRules> InitializePluralRules(JSThread *thread, const JSHandle<JSPluralRules> &pluralRules,
754514f5e3Sopenharmony_ci                                                         const JSHandle<JSTaggedValue> &locales,
764514f5e3Sopenharmony_ci                                                         const JSHandle<JSTaggedValue> &options);
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_ci    // 15.1.4 ResolvePlural ( pluralRules, n )
794514f5e3Sopenharmony_ci    static JSHandle<EcmaString> ResolvePlural(JSThread *thread, const JSHandle<JSPluralRules> &pluralRules, double n);
804514f5e3Sopenharmony_ci
814514f5e3Sopenharmony_ci    static void ResolvedOptions(JSThread *thread, const JSHandle<JSPluralRules> &pluralRules,
824514f5e3Sopenharmony_ci                                const JSHandle<JSObject> &options);
834514f5e3Sopenharmony_ci};
844514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
854514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_JS_PLURAL_RULES_H