text: import _, { result } from "lodash";
  import momentTimezone from "moment-timezone";
  import moment from "moment";
  import store from "@/store";
  
  export default {
      methods: {
          canAccess(name) {
              const permission = {...store.state.authUser.permission}
              return _.find(permission, (p) => p === name) ? true : false
          },
          setting() {
              return store.state.authUser.setting;
          },
          findSetting(key) {
              const setting = _.find(store.state.authUser.setting, (s) => s.key === key)
              return setting.value
          },
          avatarText(value) {
              if (!value) return "";
              const nameArray = value.split(" ");
              return nameArray
                  .map((word) => word.charAt(0).toUpperCase())
                  .join("");
          },
          formatPrice(value) {
              const currencyRight = this.findSetting("currency_right")
              const currencyDecimalDigits = parseInt(this.findSetting('default_currency_decimal_digits'))
              const currencyRounding = this.findSetting('default_currency_rounding')
              const currency = this.findSetting('default_currency')
  
              const val = (currencyDecimalDigits > 0) ? (value / 1).toFixed(currencyDecimalDigits).replace(",", ".") : (value / 1);
              if (currencyRight == 1) {
                  const resultRight = (currencyDecimalDigits > 0) ? val.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + " " + currency : val
                  return `${resultRight}${currency}`
              }
              const resultLeft = (currencyDecimalDigits > 0) ? val.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : val
              
              return `${currency}${resultLeft}`
          },
          translatePaymentMethod(value) {
              switch (value) {
                  case "cash":
                      return this.__("locale.payment_method_cash");
                  case "debit_card":
                      return this.__("locale.payment_method_debit_card");
                  case "credit_card":
                      return this.__("locale.payment_method_credit_card");
                  case "check":
                      return this.__("locale.payment_method_check");
                  case "transfer":
                      return this.__("locale.payment_method_transfer");
              }
          },
          numberFormat(number, fixed = 2) {
              return number
                  ? number === "N/A"
                      ? number
                      : `${parseFloat(number).toFixed(fixed)}`
                  : number
                  ? number === "N/A"
                      ? number
                      : parseFloat(number).toFixed(fixed)
                  : "—";
          },
          nFormatter(num, digits = 2) {
              var si = [
                  { value: 1, symbol: "" },
                  { value: 1e3, symbol: "k" },
                  { value: 1e6, symbol: "M" },
                  { value: 1e9, symbol: "G" },
                  { value: 1e12, symbol: "T" },
                  { value: 1e15, symbol: "P" },
                  { value: 1e18, symbol: "E" },
              ];
              var rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
              var i;
              for (i = si.length - 1; i > 0; i--) {
                  if (num >= si[i].value) {
                      break;
                  }
              }
              return (
                  (num / si[i].value).toFixed(digits).replace(rx, "$1") +
                  si[i].symbol
              );
          },
          /**
           * Helper for add a unit type to number
           * @param {String|Number} number
           * @param {String} unitFormat
           * @param {Number} fixed
           * @return String|Number
           */
          numberUnitFormat(number, unitFormat, fixed = 2) {
              return unitFormat
                  ? number
                      ? number === "N/A"
                          ? number
                          : `${parseFloat(number).toFixed(fixed)} ${unitFormat}`
                      : "—"
                  : number
                  ? number === "N/A"
                      ? number
                      : parseFloat(number).toFixed(fixed)
                  : "—";
          },
          /**
           * Helper to get format datetime
           * @param {String} number
           * @return String
           */
          dateTimeFormat(date, format = "DD/MM/YYYY - h:mm a") {
              moment.locale(this.findSetting('language'))
              moment.tz.setDefault(this.findSetting('timezone'));
              if (moment(date).isValid()) {
                  let dateWithTimeZone = momentTimezone(date);
                  return dateWithTimeZone.format(format);
              }
          },
          
          /**
           * Helper to get format date
           * @param {String} number
           * @return String
           */
          dateFormat(date, format = "DD/MM/YYYY") {
              moment.locale(this.findSetting('language'))
              moment.tz.setDefault(this.findSetting('timezone'));
              if (moment(date).isValid()) {
                  let dateWithTimeZone = momentTimezone(date);
                  // return moment.parseZone(dateWithTimeZone).format(format);
                  return dateWithTimeZone.format(format);
              }
          },
          dateHuman(date) {
              moment.locale(this.findSetting('language'))
              moment.tz.setDefault(this.findSetting('timezone'));
              let before = moment(date, "YYYY-MM-DD H:i:s");
              let now = moment(new Date(), "YYYY-MM-DD H:i:s");
              return moment(before).fromNow();
          },
          momentFromNow(_date) {
              moment.locale(this.findSetting('language'))
              moment.tz.setDefault(this.findSetting('timezone'));
              let before = moment(_date, "YYYY-MM-DD");
              let now = moment(new Date(), "YYYY-MM-DD");
              return moment(before).fromNow();
          },
          momentFormat(date = null, format = 'MMMM Do YYYY, h:mm:ss a') {
              moment.tz.setDefault(this.findSetting('timezone'));
              moment.locale(this.findSetting('language'))
              if (date) {
                  return moment(date).format(this.findSetting('date_format'))
              }
              return moment().format(format)
              
          },
          isEmpty(value) {
              return _.isEmpty(value) ? true : false
          },
          isSize(value) {
              return _.size(value)
          },
          translate (key) {
              return this.__(key);
          },
          formatStatus(value) {
              return value ? this.__('locale.yes') : this.__('locale.no')
          },
          genUniqueKey (obj) {
              const UNIQUE_KEY_PROP = '__unique_key_prop__'
              const KEY_PREFIX = '__key_prefix__' + Date.now() + '_'
              let uid = 0
  
              const isObject = val => val !== null && typeof val === 'object'
              if (isObject(obj)) {
                  if (UNIQUE_KEY_PROP in obj) {
                  return obj[UNIQUE_KEY_PROP]
                  }
                  const value = KEY_PREFIX + uid++
                  Object.defineProperty(obj, UNIQUE_KEY_PROP, { value })
                  return value
              }
              return obj
          },
          resolveReplaceOperator (oldString, newString) {
              let _string = this.__('locale.work_order_expired')
              _string = _string.replace(oldString, newString)
              return _string;
          },
          resolvePaginationTranslate(dataMeta) {
              let _string = this.__("datatable.sInfo");
              _string = _string.replace("_START_", dataMeta.from);
              _string = _string.replace("_END_", dataMeta.to);
              _string = _string.replace("_TOTAL_", dataMeta.of);
              return _string;
          },
          resolveStatusVariant(status) {
              if (_.isEmpty(status)) {
                  return "success";
              }
              return "secondary";
          },
          resolveStatusTranslate(status) {
              if (_.isEmpty(status)) {
                  return this.translate("locale.active");
              }
              return this.translate("locale.inactive");
          },
          resolveStateAppointment(state) {
              if (state === 'pending') return this.__('locale.appointment_state_pending')
              if (state === 'canceled') return this.__('locale.appointment_state_canceled')
              if (state === 'confirmed') return this.__('locale.appointment_state_confirmed')
              if (state === 'assisted') return this.__('locale.appointment_state_assisted')
              if (state === 'unassisted') return this.__('locale.appointment_state_unassisted')
          },
          resolveStateAppointmentColor(state) {
              if (state === 'pending') return 'danger'
              if (state === 'canceled') return 'danger'
              if (state === 'confirmed') return 'success'
              if (state === 'assisted') return 'primary'
              if (state === 'unassisted') return 'dark'
          },
          resolveTypeDocumentTranslate(type) {
              switch (type) {
                  case "RUT":
                      return `${this.__('locale.patient_document_type')} (${this.__("locale.document_type_rut")})`
                      break;
                  case "Pasaporte":
                      return `${this.__('locale.patient_document_type')} (${this.__("locale.document_type_passport")})`
                      break;
                  case "Otro":
                      return `${this.__('locale.patient_document_type')} (${this.__("locale.document_type_other")})`
                      break;
              }
  
             
          },
          formatActive(value) {
              return value ? this.__('locale.yes') : this.__('locale.no')
          },
      },
  };
  