<template>
  <div class="label-tag">
    <template v-if="mergedConfig">
      <div
        v-for="(label, i) in mergedConfig.data"
        :key="label"
        class="label-item"
      >
        {{ label }}
        <div
          :style="`background-color: ${mergedConfig.colors[i % mergedConfig.colors.length]};`"
        ></div>
      </div>
    </template>
  </div>
</template>

<script>
export default {
  name: "LabelTag",
  props: {
    config: {
      type: Object,
      default: () => [],
    },
  },
  data() {
    return {
      defaultConfig: {
        /**
         * @description Label data
         * @type {Array<String>}
         * @default data = []
         * @example data = ['label1', 'label2']
         */
        data: [],
        /**
         * @description Label color (Hex|Rgb|Rgba|color keywords)
         * @type {Array<String>}
         * @default colors = ['#00baff', '#3de7c9', '#fff', '#ffc530', '#469f4b']
         * @example colors = ['#666', 'rgb(0, 0, 0)', 'rgba(0, 0, 0, 1)', 'red']
         */
        colors: ["#00baff", "#3de7c9", "#fff", "#ffc530", "#469f4b"],
      },

      mergedConfig: null,
    };
  },
  watch: {
    config() {
      const { mergeConfig } = this;

      mergeConfig();
    },
  },
  mounted() {
    const { mergeConfig } = this;

    mergeConfig();
  },
  methods: {
    mergeConfig() {
      let { config, defaultConfig } = this;

      this.mergedConfig = { ...defaultConfig, ...config };
    },
  },
};
</script>

<style lang="less">
.label-tag {
  display: flex;
  justify-content: center;
  align-items: center;

  .label-item {
    margin: 5px;
    font-size: 15px;
    display: flex;
    align-items: center;

    div {
      width: 12px;
      height: 12px;
      margin-left: 5px;
    }
  }
}
</style>
