# Copyright 2010-2021, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#     * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
#     * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

load(
    "//:build_defs.bzl",
    "mozc_cc_library",
    "mozc_cc_test",
)
load(
    "//data_manager:mozc_data.bzl",
    "magic_number",
    "mozc_dataset",
)

package(default_visibility = ["//visibility:public"])

# "\xEFMOZC\r\n"
MOZC_DATASET_MAGIC_NUMBER = magic_number([
    0xEF,  # \xEF
    0x4D,  # M
    0x4F,  # O
    0x5A,  # Z
    0x43,  # C
    0x0D,  # <CR>
    0x0A,  # <LF>
])

mozc_dataset(
    name = "mozc_dataset_for_oss",
    outs = [
        "mozc.data",
        "mozc_data.inc",
        "pos_list.inc",
    ],
    a11y_description_src = (
        "//data/a11y_description:a11y_description_data.tsv"
    ),
    boundary_def = "//data/rules:boundary.def",
    cforms = "//data/rules:cforms.def",
    collocation_src = (
        "//data/dictionary_oss:collocation.txt"
    ),
    collocation_suppression_src = (
        "//data/dictionary_oss:collocation_suppression.txt"
    ),
    connection_single_column_src = "//data/dictionary_oss:connection_single_column.txt",
    dictionary_srcs = [
        "//data/dictionary_oss:dictionary_data",
        "//data/dictionary_oss:reading_correction.tsv",
        "//data/dictionary_manual:domain.txt",
    ],
    emoji_src = "//data/emoji:emoji_data.tsv",
    emoticon_categorized_src = (
        "//data/emoticon:categorized.tsv"
    ),
    emoticon_src = "//data/emoticon:emoticon.tsv",
    id_def = "//data/dictionary_oss:id.def",
    magic = MOZC_DATASET_MAGIC_NUMBER,
    pos_group_def = (
        "//data/rules:user_segment_history_pos_group.def"
    ),
    pos_matcher_rule_def = (
        "//data/rules:pos_matcher_rule.def"
    ),
    reading_correction_src = (
        "//data/dictionary_oss:reading_correction.tsv"
    ),
    segmenter_def = "//data/rules:segmenter.def",
    segmenter_generator_srcs = ["gen_oss_segmenter_bitarray_main.cc"],
    single_kanji_src = (
        "//data/single_kanji:single_kanji.tsv"
    ),
    sorting_map = "//data/rules:sorting_map.tsv",
    special_pos = "//data/rules:special_pos.def",
    suffix = "//data/dictionary_oss:suffix.txt",
    suggestion_filter_src = (
        "//data/dictionary_oss:suggestion_filter.txt"
    ),
    symbol_ordering_rule = "//data/symbol:ordering_rule.txt",
    symbol_src = "//data/symbol:symbol.tsv",
    tag = "oss",
    usage_dict = "@ja_usage_dict//:usage_dict.txt",
    use_1byte_cost = "false",
    user_pos_def = "//data/rules:user_pos.def",
    variant_rule = "//data/single_kanji:variant_rule.txt",
    varname = "kOssMozcDataSet",
    zero_query_def = "//data/zero_query:zero_query.def",
    zero_query_number_def = (
        "//data/zero_query:zero_query_number.def"
    ),
)

mozc_cc_library(
    name = "oss_data_manager",
    srcs = [
        "oss_data_manager.cc",
        ":mozc_dataset_for_oss@header",
    ],
    hdrs = ["oss_data_manager.h"],
    local_defines = [
        "MOZC_DATASET_MAGIC_NUMBER_LENGTH=%d" % MOZC_DATASET_MAGIC_NUMBER.length,
    ],
    deps = [
        "//base:embedded_file",
        "//data_manager",
        "@com_google_absl//absl/log:check",
        "@com_google_absl//absl/strings",
    ],
)

mozc_cc_test(
    name = "oss_data_manager_test",
    size = "large",
    srcs = [
        "oss_data_manager_test.cc",
        ":mozc_dataset_for_oss@segmenter_inl_header",
    ],
    data = [
        "//data/dictionary_oss:connection_single_column.txt",
        "//data/dictionary_oss:dictionary_data",
        "//data/dictionary_oss:suggestion_filter.txt",
    ],
    deps = [
        ":oss_data_manager",
        "//data_manager:data_manager_test_base",
        "//testing:gunit_main",
        "//testing:mozctest",
    ],
)
