1/*
2 * Copyright (c) 2021-2022 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_LIST_FORMAT_H
17#define ECMASCRIPT_JS_LIST_FORMAT_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
26#include "unicode/listformatter.h"
27
28namespace panda::ecmascript {
29enum class ListTypeOption : uint8_t {
30    CONJUNCTION = 0x01,
31    DISJUNCTION,
32    UNIT,
33    EXCEPTION
34};
35
36enum class ListStyleOption : uint8_t {
37    LONG = 0x01,
38    SHORT,
39    NARROW,
40    EXCEPTION
41};
42
43class JSListFormat : public JSObject {
44public:
45    CAST_CHECK(JSListFormat, IsJSListFormat);
46
47    static constexpr size_t LOCALE_OFFSET = JSObject::SIZE;
48    ACCESSORS(Locale, LOCALE_OFFSET, ICU_LIST_FORMAT)
49    ACCESSORS(IcuLF, ICU_LIST_FORMAT, BIT_FIELD_OFFSET)
50    ACCESSORS_BIT_FIELD(BitField, BIT_FIELD_OFFSET, LAST_OFFSET)
51    DEFINE_ALIGN_SIZE(LAST_OFFSET);
52
53    // define BitField
54    static constexpr size_t TYPE_BITS = 3;
55    static constexpr size_t STYLE_BITS = 3;
56    FIRST_BIT_FIELD(BitField, Type, ListTypeOption, TYPE_BITS)
57    NEXT_BIT_FIELD(BitField, Style, ListStyleOption, STYLE_BITS, Type)
58
59    DECL_VISIT_OBJECT_FOR_JS_OBJECT(JSObject, LOCALE_OFFSET, BIT_FIELD_OFFSET)
60    DECL_DUMP()
61
62    icu::ListFormatter *GetIcuListFormatter() const;
63
64    static void FreeIcuListFormatter(void *env, void *pointer, [[maybe_unused]] void* hint);
65
66    static void SetIcuListFormatter(JSThread *thread, const JSHandle<JSListFormat> obj,
67                                    icu::ListFormatter *icuListFormatter, const NativePointerCallback &callback);
68
69    static JSHandle<TaggedArray> GetAvailableLocales(JSThread *thread);
70
71    // 13. InitializeListFormat ( listformat, locales, options )
72    static JSHandle<JSListFormat> InitializeListFormat(JSThread *thread, const JSHandle<JSListFormat> &listFormat,
73                                                       const JSHandle<JSTaggedValue> &locales,
74                                                       const JSHandle<JSTaggedValue> &options);
75
76    // 13.1.3 FormatList ( listFormat, list )
77    static JSHandle<EcmaString> FormatList(JSThread *thread, const JSHandle<JSListFormat> &listFormat,
78                                            const JSHandle<JSArray> &listArray);
79
80    // 13.1.4 FormatListToParts ( listFormat, list )
81    static JSHandle<JSArray> FormatListToParts(JSThread *thread, const JSHandle<JSListFormat> &listFormat,
82                                                const JSHandle<JSArray> &listArray);
83
84    // 13.1.5 StringListFromIterable ( iterable )
85    static JSHandle<JSTaggedValue> StringListFromIterable(JSThread *thread, const JSHandle<JSTaggedValue> &iterable);
86
87    static void ResolvedOptions(JSThread *thread, const JSHandle<JSListFormat> &listFormat,
88                                const JSHandle<JSObject> &options);
89};
90}  // namespace panda::ecmascript
91#endif  // ECMASCRIPT_JS_LIST_FORMAT_H