import GlBadge from '../components/base/badge/badge';
import GlCollapsibleListbox from '../components/base/new_dropdowns/listbox/listbox';
import GlFormInput from '../components/base/form/form_input/form_input';
import GlTable from '../components/base/table/table';
import TOKENS_DEFAULT from './build/json/tokens.json';
import TOKENS_DARK from './build/json/tokens.dark.json';
import __vue_normalize__ from 'vue-runtime-helpers/dist/normalize-component.js';

var script = {
  name: 'TokensTable',
  TOKENS_DEFAULT,
  TOKENS_DARK,
  components: {
    GlBadge,
    GlCollapsibleListbox,
    GlFormInput,
    GlTable
  },
  fields: [{
    key: 'description',
    label: 'Description'
  }, {
    key: 'value',
    label: 'Value'
  }],
  data() {
    return {
      filter: null,
      modes: [{
        value: 'default',
        text: 'Default'
      }, {
        value: 'dark',
        text: 'Dark'
      }],
      selectedMode: 'default',
      tokens: {
        default: this.$options.TOKENS_DEFAULT,
        dark: this.$options.TOKENS_DARK
      }
    };
  },
  computed: {
    items() {
      return this.transformTokensToTableRows(this.tokens[this.selectedMode]);
    }
  },
  methods: {
    isColor(type) {
      return type === 'color';
    },
    isAliasValue(value) {
      return typeof value === 'string' && value.includes('{');
    },
    isAliasObject(value) {
      return typeof value === 'object' && Object.values(value).some(val => this.isAliasValue(val));
    },
    getAliasValueName(value) {
      if (this.isAliasValue(value)) {
        return value.slice(1, -1);
      }
      return value;
    },
    getValueLabel(token) {
      const {
        value
      } = token.original;
      if (this.isAliasObject(value)) {
        return this.getAliasValueName(value[this.selectedMode]);
      }
      if (this.isAliasValue(value)) {
        return this.getAliasValueName(value);
      }
      return token.value;
    },
    transformTokenToTableColumns(token) {
      return {
        name: token.path.filter(Boolean).join('.'),
        type: token.$type,
        value: token.value,
        valueLabel: this.getValueLabel(token),
        deprecated: token.deprecated,
        description: token.comment
      };
    },
    transformTokensToTableRows(tokens) {
      const tokensArray = [];
      Object.keys(tokens).forEach(key => {
        const token = tokens[key];
        if (token.value) {
          tokensArray.push(this.transformTokenToTableColumns(token));
        } else {
          tokensArray.push(...this.transformTokensToTableRows(token));
        }
      });

      // Sort tokensArray so deprecated items are last
      tokensArray.sort((a, b) => {
        if (a.deprecated && !b.deprecated) {
          return 1;
        }
        if (!a.deprecated && b.deprecated) {
          return -1;
        }
        return 0;
      });
      return tokensArray;
    }
  }
};

/* script */
const __vue_script__ = script;

/* template */
var __vue_render__ = function () {var _vm=this;var _h=_vm.$createElement;var _c=_vm._self._c||_h;return _c('div',[_c('div',{staticClass:"gl-display-flex gl-align-items-center gl-gap-3 gl-mb-5"},[_c('gl-form-input',{attrs:{"placeholder":"Type to search"},model:{value:(_vm.filter),callback:function ($$v) {_vm.filter=$$v;},expression:"filter"}}),_vm._v(" "),_c('gl-collapsible-listbox',{attrs:{"id":"component-listbox","selected":_vm.selectedMode,"items":_vm.modes},on:{"search":function($event){_vm.query = $event;}},model:{value:(_vm.selectedMode),callback:function ($$v) {_vm.selectedMode=$$v;},expression:"selectedMode"}})],1),_vm._v(" "),_c('gl-table',{attrs:{"filter":_vm.filter,"items":_vm.items,"fields":_vm.$options.fields,"tbody-tr-attr":function (item) { return ({ id: item.name }); },"hover":"","stacked":"sm"},scopedSlots:_vm._u([{key:"cell(description)",fn:function(ref){
var ref_item = ref.item;
var name = ref_item.name;
var deprecated = ref_item.deprecated;
var description = ref_item.description;
return [_c('code',{staticClass:"gl-font-base gl-text-strong"},[_vm._v("\n        "+_vm._s(name)+"\n      ")]),_vm._v(" "),(deprecated)?_c('gl-badge',{attrs:{"variant":"danger"}},[_vm._v("Deprecated")]):_vm._e(),_vm._v(" "),(description)?_c('div',{staticClass:"gl-mt-3 gl-text-subtle"},[_vm._v("\n        "+_vm._s(description)+"\n      ")]):_vm._e()]}},{key:"cell(value)",fn:function(ref){
var ref_item = ref.item;
var type = ref_item.type;
var value = ref_item.value;
var valueLabel = ref_item.valueLabel;
return [_c('div',{staticClass:"gl-display-flex gl-align-items-center gl-gap-3"},[(_vm.isColor(type))?_c('div',{staticClass:"gl-w-5 gl-h-5 gl-rounded-base",style:({ 'background-color': value })}):_vm._e(),_vm._v(" "),_c('code',{staticClass:"gl-font-base gl-text-strong"},[_vm._v(_vm._s(valueLabel))])])]}}])})],1)};
var __vue_staticRenderFns__ = [];

  /* style */
  const __vue_inject_styles__ = undefined;
  /* scoped */
  const __vue_scope_id__ = undefined;
  /* module identifier */
  const __vue_module_identifier__ = undefined;
  /* functional template */
  const __vue_is_functional_template__ = false;
  /* style inject */
  
  /* style inject SSR */
  
  /* style inject shadow dom */
  

  
  const __vue_component__ = __vue_normalize__(
    { render: __vue_render__, staticRenderFns: __vue_staticRenderFns__ },
    __vue_inject_styles__,
    __vue_script__,
    __vue_scope_id__,
    __vue_is_functional_template__,
    __vue_module_identifier__,
    false,
    undefined,
    undefined,
    undefined
  );

export default __vue_component__;
